Ich habe ein Produktmodell mit vielen Abschnitten, und ein Abschnitt kann zu vielen Produkten gehören.
Das Abschnittsmodell hat Unterklassen von Feature, Standard und Option.
Meine Modelle sind:
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
In meinem Produktcontroller kann ich dies tun:
@product.sections.build
Ich möchte zu den Unterklassen wie etwas in der Lage sein:
@product.features.build
@product.standards.build
@product.options.build
Aber es ist nur Fehler mit "undefined Methode 'Features" usw.
Kann mir bitte jemand sagen, wie ich das mache?
Angenommen, Sie haben eine has_and_belongs_to_many Join-Tabelle mit dem Namen "products_sections". Sie benötigen diese zusätzlichen Verknüpfungen in Ihrem Prodcut-Modell:
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
Produkt hat diese Methoden nicht, weil sie nie definiert wurden. Sie müssen Beziehungen in Ihrer Produktklasse definieren, um die Methoden features / standards / options zu erhalten
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
wird Ihnen ein besseres Verständnis davon geben, was die Definition von Beziehungen für Sie bedeutet