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

I know this is wrong:

@exams = Exam.find(:all, :conditions => [“class.student_id = ?”,
current_user.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
student.

Any input?

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

class Exam < ActiveRecordBase
has_and_belongs_to_many :students
end

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
recommend
reading AWDwRv2 chapter 18.

Good luck,

-Conrad

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.

So:

user.rb model:
has_many :exams
belongs_to :class

exam.rb model:
belongs_to :user

controller:
user = User.find(current_user.id)
user.exams #=> [1,Math], [2,English], etc.

Cheers,
Zach I.
→ Blog – http://www.zachinglis.com
→ Company – http://www.lt3media.com
→ Portfolio – http://portfolio.zachinglis.com