Rails Polymorphic Associations -


total newbie rails , programming in general, please forgive me if answer question seems glaringly obvious. i've been doing lot of reading including the ror guides can't seem find scenario specific following situation:

i have 3 models scaffolded under 1 user model, , trying link them so:

class user < activerecord::base    has_many :malls, :dependent => :destroy end  class mall < activerecord::base    belongs_to :user    has_many :stores, :dependent => :destroy    has_many :cakes, :as => :cake_poly, :dependent => :destroy end  class store < activerecord::base    belongs_to :mall    has_many :cakes, :as => :cake_poly, :dependent => :destroy end  class cakes < activerecord::base    belongs_to :cake_poly, :polymorphic => true end 

will work? if does, there better way implement this? if not, how else can implement associations?

the idea each user may have many malls, each mall may have many stores, , both malls , stores may have many cakes.

a key question design intent - want achieve?

you should not use polymorphic relationship if aggregate cakes available in mall's shops mall model. can use following relationship achieve such design:

class mall < activerecord::base   has_many :stores   has_many :cakes, :through => :stores end 

the polymorphic approach perfect choice if 2 sets (cakes in mall, cakes in mall's stores) don't correlate.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -