ruby on rails - Setting up a Has_Many :through Association -


on application i'm working on, i'm stuck on setting associations between 3 models ensure referential integrity. have event model, building model, , room model. association in real life pretty intuitive. event can in 1 building , 1 room. building can have multiple rooms.

here's have set now. however, how can events specify room if belong buildings, , foreign key room in events table? use has_many :through relationship? practice store both building , room foreign keys in event table, rooms owned buildings? conditional relationship requires building specified before allowing room specified (some buildings have 2 rooms, others have 20, example)

sorry i'm unclear on this. in advance help!

    class event < activerecord::base     belongs_to :building      end      class building < activerecord::base     has_many :events     has_many :rooms      end      class room < activerecord::base     belongs_to :building      end 

i think best way handle following:

class event < activerecord::base  belongs_to :room  has_one :building, :through => :room end  class building < activerecord::base  has_many :events  has_many :rooms end  class room < activerecord::base  belongs_to :building end 

so can use has_one :through specify event owns hotel


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -