0

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!

user1175969
  • 540
  • 6
  • 19

1 Answers1

1

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?

Community
  • 1
  • 1
Pholochtairze
  • 1,836
  • 1
  • 14
  • 18