Routes, controllers, and intentions

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:

but this lists the rosters for all courses.

if I use 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?

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

has_many rosters
has_many students through rosters

has_many rosters
has_many courses through rosters

belongs_to course
belongs_to student

You need to pass the course_id to the roster in a param, like:


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 = ?’,

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs