2009-06-16 5 views
7

Ho un modello di prodotto con molte sezioni e una sezione può appartenere a molti prodotti.Associazione HABTM associata all'ereditarietà di una tabella singola

Il modello di sezione include sottoclassi di Caratteristica, Standard e Opzione.

I miei modelli sono:

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 

Nel mio controller prodotti posso fare questo:

@product.sections.build 

Voglio essere in grado di raggiungere le sottoclassi come qualcosa di simile:

@product.features.build 

@product.standards.build 

@product.options.build 

Ma si sbaglia solo con "caratteristiche del metodo non definito" "ecc.

Per favore qualcuno può dirmi come fare questo?

risposta

11

Supponendo che si dispone di un has_and_belongs_to_many uniscono tabella con il nome "products_sections", ciò che ti serve sono queste associazioni aggiuntive nel tuo modello 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