단일 테이블 상속을 사용하는 카드 게임 응용 프로그램이 있습니다. 나는 class Card
이고, 데이터베이스 테이블 cards
은 type
이고, 서브 클래스 수는 Card
입니다 (인수를 위해서 class Foo < Card
및 class Bar < Card
포함).레일 : 단일 테이블 상속 및 모델 하위 디렉터리
Foo
은 게임 원본 인쇄 카드이고 Bar
은 확장 카드입니다. 내 모델을 합리화하기 위해, 나는 그래서 같은 디렉토리 구조를 만들었습니다
app/
+ models/
+ card.rb
+ base_game/
+ foo.rb
+ expansion/
+ bar.rb
그리고 수정으로 environment.rb을 포함 :
Rails::Initializer.run do |config|
config.load_paths += Dir["#{RAILS_ROOT}/app/models/**"]
end
그러나, 내 응용 프로그램이 데이터베이스에서 카드를 읽을 때
ActiveRecord::SubclassNotFound (The single-table inheritance mechanism failed to locate the subclass: 'Foo'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Card.inheritance_column to use another column for that information.)
는이 작품을 만들기 위해, 또는 내가 플랫 디렉토리 구조에 운명입니다 가능 : 레일은 다음과 같은 예외를 throw?
나는 두려움을 가지고 있습니다. "Foo 클래스를 Card 모듈 안에 중첩"한다는 의미에 대해 더 많은 설명 (일부 스텁 코드는 좋을 것입니다)을 게시 할 수 있습니까? 당신은 나머지 대답에서 그것을 언급하지 않는 것 같습니다. – Chowlett
나는 너를 이해하고 있다고 생각한다. 너의 첫 줄에 "BaseGame module 안에"있다는 뜻이야? 이렇게하면 데이터베이스의 유형 열에 무엇이 포함되어야합니까? "Foo"또는 "BaseGame :: Foo"? – Chowlett
@Chris, 예, 당신 말이 맞습니다. type 컬럼에 "BaseGame :: Foo"로 기록됩니다. 그러나 이것을 명시해서는 안되며, 레일스가 모든 것을 처리 할 것입니다. – Ceilingfish