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

**URL:** <https://www.ruby-forum.com/t/how-would-i-find-all-exams-that-belong-to-a-student/111303>\
**Category:** Rails\
**Created:** [August 5, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/how-would-i-find-all-exams-that-belong-to-a-student/111303 "2007-08-05T13:13:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 5, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/how-would-i-find-all-exams-that-belong-to-a-student/111303/1 "2007-08-05T13:13:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 5, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/how-would-i-find-all-exams-that-belong-to-a-student/111303/2 "2007-08-05T13:44:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 5, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/how-would-i-find-all-exams-that-belong-to-a-student/111303/3 "2007-08-05T19:07:22Z")

</div>

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.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 5, 2007, 2:28pm UTC](https://www.ruby-forum.com/t/how-would-i-find-all-exams-that-belong-to-a-student/111303/4 "2007-08-05T14:28:58Z")

</div>

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](http://www.zachinglis.com)  
â†’ Company – [http://www.lt3media.com](http://www.lt3media.com)  
â†’ Portfolio – [http://portfolio.zachinglis.com](http://portfolio.zachinglis.com)
