How would I find all "Exams" that belong to a "Student"?

I know this is wrong:

@exams = Exam.find(:all, :conditions => [“class.student_id = ?”,])

How would I correct the above to make it work?

I know I can’t have class.student_id, since that’s not a column name in
the database table.

Beyond that, I have no idea how to find all exams that belongs to a

Any input?

Hi, if you have the following models
class Student < ActiveRecordBase
has_and belongs_to_many :exams

class Exam < ActiveRecordBase
has_and_belongs_to_many :students

What exams did a student take

student = Student.find( student_id )
exams_taken = student.exams

For a better explaination of Rails relationships, I would highly
reading AWDwRv2 chapter 18.

Good luck,


U can do: student.exams assuming u have student has-many exams
declaration in ur student model

Sent from my iPhone

On Aug 5, 2007, at 4:13 AM, Bob S.
<[email protected]

Leave class out of it, as the id is not relative to the class.


user.rb model:
has_many :exams
belongs_to :class

exam.rb model:
belongs_to :user

user = User.find(
user.exams #=> [1,Math], [2,English], etc.

Zach I.
→ Blog –
→ Company –
→ Portfolio –