Reputation: 3395
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.
Upvotes: 0
Views: 163
Reputation: 2035
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
Upvotes: 4