ruby - Rails 4 HABTM has_many :through -
team, looking specific (newbie) situation on rails 4 association. have 3 models:
class brand < activerecord::base has_many :lines, dependent: :destroy has_many :products, through: :lines, dependent: :destroy end class line < activerecord::base belongs_to :brand has_and_belongs_to_many :products end class product < activerecord::base has_and_belongs_to_many :lines has_many :brands, through: :lines end
this configuration works when trying check products
under specific brand
(or line
) , viceversa: different brands
(or lines
) available specific product
. however, when comes delete/destroy there issue. getting rspec error:
activerecord::hasmanythroughcantassociatethroughhasoneormanyreflection: cannot modify association 'brand#products' because source reflection class 'product' associated 'line' via :has_and_belongs_to_many.
we have made research on exception, checked rails api, no luck, examples found showing different model configuration. what's missing on approach?
appreciate guys!
in opinion, should this:
class brand < activerecord::base has_many :lines, dependent: :destroy has_many :products, through: :lines, dependent: :destroy end class line < activerecord::base belongs_to :brand has_and_belongs_to_many :products end class product < activerecord::base belongs_to :brand, through: :line has_and_belongs_to_many :lines end
and in migrations:
create_table :brands , force: true |t| t.string :name ... t.timestamps null: false end create_table :lines , force: true |t| t.string :name t.belongs_to :brand ... t.timestamps null: false end create_table :products , force: true |t| t.string :name ... t.timestamps null: false end create_table :line_products, force: true, id: false |t| t.belongs_to :line, index: true t.belongs_to :product, index: true end
i hope help.
Comments
Post a Comment