Reputation: 570
I have the classes Student, Classroom and Course. A Student has and belongs to many classrooms, and a Classroom has and belongs to many courses.
How do I retrieve all the courses associated with a Student? Also, how do I retrieve all students associated with a course?
Thanks!
Upvotes: 0
Views: 35
Reputation: 1854
Use has_many ... :through
in your model (models/student.rb
):
has_many :courses, :through => :classrooms
has_many :classrooms
Then you can use :
student.courses
Some useful stackoverflow link : When should one use a "has_many :through" relation in Rails?
Upvotes: 1