ruby on rails - Using multiple has_many associations on one model -
i working in rails app company has_many orders. order has name , it. order can have_many television_spots, radio_placements, , newspaper_placements, data in these tables not same have split up. if order has_many radio_placements not have television_spots or newspaper_placements. question bad practice have has_many relationship on model if not exist?, better better make company have_many television_orders , television_order have_many television_spots? , company have_many radio_orders , radio_order have_many radio_placements, , on. think first option dryer using second strategy result in cleaner more understandable code app grows , data changes.
it's not bad practice have has_many association not have models associated. real question object type radio_placements
, television_spots
, newspaper_placements
should associated to. if should in fact related order
model, place associations there. understanding of question/data shape appear want these relationships on order
model. like:
class company has_many :orders has_many :television_spots, through: :orders has_many :radio_placements, through: :orders has_many :newspaper_placements, through: :orders end class order has_many :television_spots has_many :radio_placements has_many :newspaper_placements end
hopefully helps.
Comments
Post a Comment