Forum: Ruby on Rails routes, controllers, and intentions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
soldier.coder (Guest)
on 2009-04-27 15:06
(Received via mailing list)
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

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?
Ar C. (Guest)
on 2009-04-27 21:21
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

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.
This topic is locked and can not be replied to.