Forum: Ruby on Rails Best way about doing this?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Benson L. (Guest)
on 2006-03-27 04:22
Hi All,
I'm super a super ruby nuby, so bare with me.  I am working on a small
app that has a particular form that I am having a rough time figuring
out the best to way to handle.

Basically here it is, each school has one or more classrooms, and each
classroom can have one or more teachers.

Here's what I am trying to do (edit a school):
 - click on a link to edit a School
 - The school form loads and with it, all it's classrooms are listed (I
have this part ok)
 - part I'm trying to think of how to do is:
  - for each classroom, grabbing all the teachers...Would do this in the
controller or the rhtml file itself?  If in the controller, how?  It's
probably a stupid question, but I can't see it.  Can you do a loop
through each of the classrooms and assign them an array of teachers?

here is the controller for the form so far:
	def view_school
		@school = School.find(params[:id])
		@classrooms = Classroom.find(:all,
		:conditions => ["school_id = ?", params[:id])
Tom D. (Guest)
on 2006-03-27 04:51
(Received via mailing list)
It should be tied to your model.  If you are using the built-in
associations, you would have

  class Classroom

  has_many :teachers

For more info on ActiveRecord Associations visit:

You could loop through your @classrooms within your view using something

<% @classrooms.each do |classroom| %>
<% end %>

Then for each classroom in your view, you could access the array of
teachers using:


Hope that helps.

On 3/26/06, Benson L. <removed_email_address@domain.invalid> wrote:
>  - The school form loads and with it, all it's classrooms are listed (I
>                 @classrooms = Classroom.find(:all,
>                 :conditions => ["school_id = ?", params[:id])
>         end
> --
> Posted via
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid

Tom D.
This topic is locked and can not be replied to.