# Routes, controllers, and intentions

**URL:** <https://www.ruby-forum.com/t/routes-controllers-and-intentions/166566>\
**Category:** Rails\
**Created:** [April 27, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/routes-controllers-and-intentions/166566 "2009-04-27T13:06:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![soldier.coder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@soldier.coder](https://www.ruby-forum.com/u/soldier.coder)\
**Post date:** [April 27, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/routes-controllers-and-intentions/166566/1 "2009-04-27T13:06:36Z")

</div>

I have the following tables: courses, students, and rosters. The  
rosters table references the courses and students tables. With  
default routes in place, to list the students in a course I would use:  
[http://localhost:3000/rosters/list](http://localhost:3000/rosters/list)

but this lists the rosters for all courses.

if I use [http://localhost:3000/rosters/list/1](http://localhost:3000/rosters/list/1)

rails looks in the rosters table for entry with the id =1. How do I  
tell it I want all the entries where course\_id = 1?

---

<div class="post-metadata">

**Author:** ![soldier.coder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@soldier.coder](https://www.ruby-forum.com/u/soldier.coder)\
**Post date:** [April 27, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/routes-controllers-and-intentions/166566/2 "2009-04-27T19:21:56Z")

</div>

With default routing in place, it would seem to me to be something on  
the order of the following, given courses, students, and rosters:

course  
has\_many rosters  
has\_many students through rosters

student  
has\_many rosters  
has\_many courses through rosters

roster  
belongs\_to course  
belongs\_to student

* * *

You need to pass the course\_id to the roster in a param, like:

[http://localhost:3000/rosters?course\_id=1](http://localhost:3000/rosters?course_id=1)

which would allow a roster to determine which course it was for, and  
retrieve all the appropriate students.

def show  
@rosters = Roster.find(:all, :conditions =\> [‘course\_id = ?’,  
params[:course\_id]])  
end

There are many other ways to organize and get at this information, such  
as a partial for the course ‘show’ form, like  
‘\_course\_students.html.erb’, which could just loop through the  
@course.students collection to display the list of students.
