2009-06-16 5 views
7

J'ai un modèle de produit comportant de nombreuses sections et une section peut appartenir à de nombreux produits.Association HABTM associée à l'héritage d'une table unique

Le modèle de section a des sous-classes de fonctionnalité, standard et option.

Mes modèles sont les suivants:

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 

Dans mon contrôleur de produits que je peux le faire:

@product.sections.build 

Je veux être en mesure d'obtenir les sous-classes comme quelque chose comme ceci:

@product.features.build 

@product.standards.build 

@product.options.build 

Mais il ne s'agit que d'erreurs avec "caractéristiques de la méthode non définies" "etc.

S'il vous plaît quelqu'un peut-il me dire comment faire cela?

Répondre

11

En supposant que vous avez un has_and_belongs_to_many joindre table avec le nom "products_sections", ce que vous auriez besoin sont ces associations supplémentaires dans votre modèle 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 
Questions connexes