2009-06-16 29 views
7

私は多くのセクションを持つ製品モデルを持っており、セクションは多くの製品に属することができます。単一テーブル継承に関連付けられたHABTM関連

セクションモデルには、Feature、Standard、Optionのサブクラスがあります。

私のモデルは以下のとおりです。

@product.features.build 

@product.standards.build 

@product.options.build 

@product.sections.build 

が、私はこのような何かのようにサブクラスを取得できるようにしたい:私はこれを行うことができ、私の製品コントローラで

class Product < ActiveRecord::Base 
has_and_belongs_to_many :categories 
has_and_belongs_to_many :sections  
end 

class Section < ActiveRecord::Base 
has_and_belongs_to_many :products 
end 

class Feature < Section 
end 

class Standard < Section 
end 

class Option < Section 
end 

しかし、 "未定義のメソッド 'の機能などではエラーになります。

どうすればいいですか?

答えて

0

製品には決して定義されていない原因がありません。あなたはhas_and_belongs_to_manyアソシエーションに参加していると仮定すると、あなたに

11

を与えているあなたの関係を定義するもののより良い理解を得られます

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

機能/標準/オプションの方法を得るためにあなたの製品のクラスの関係を定義する必要がありますテーブルに「products_sections」という名前がある場合は、Prodcutモデルのこれらの追加の関連付けが必要です。

class Product < ActiveRecord::Base 
has_and_belongs_to_many :sections 
has_and_belongs_to_many :features, association_foreign_key: 'section_id', join_table: 'products_sections' 
has_and_belongs_to_many :standards, association_foreign_key: 'section_id', join_table: 'products_sections' 
has_and_belongs_to_many :options, association_foreign_key: 'section_id', join_table: 'products_sections' 
end 
関連する問題