views:

67

answers:

1

I have a "tree-like" structure to my database in an app I am writing so that:

training has_many class_times

and

class_time has_many reservations

Is there a way to look up all reservations under a a given training? I could iterate through all of class times/add a foreign key, of course, but for some reason I have this little voice in the back of my head that says I might not need a foreign key for this.

+3  A: 
class Training < ActiveRecord::Base
  has_many :class_times
  has_many :reservations, :through => :class_times
end

class ClassTime < ActiveRecord::Base
  has_many :reservations
end

then you can do:

training = Training.find(:first)
training.reservations
Maximiliano Guzman
for some reason I thought the :through association needed more info, brain freeze for a second. Thanks for the help! brilliant!
BushyMark