ruby-on-rails – 与单表继承关联的HABTM关联
发布时间:2020-12-17 04:25:49 所属栏目:百科 来源:网络整理
导读:我有一个产品型号,有许多部分,一个部分可以属于许多产品. 剖面模型具有Feature,Standard和Option的子类. 我的模特是: class Product ActiveRecord::Base has_and_belongs_to_many :categories has_and_belongs_to_many :sections endclass Section ActiveRe
我有一个产品型号,有许多部分,一个部分可以属于许多产品.
剖面模型具有Feature,Standard和Option的子类. 我的模特是: 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 在我的产品控制器中,我可以这样做: @product.sections.build 我想能够像这样的东西到达子类: @product.features.build @product.standards.build @product.options.build 但它只是错误的“未定义的方法’功能’”等. 请有人能告诉我怎么做吗? 解决方法
假设您有一个名为“products_sections”的has_and_belongs_to_many连接表,那么您需要的是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,join_table: 'products_sections' has_and_belongs_to_many :options,join_table: 'products_sections' end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |