Best way about doing this?


#1

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])
end


#2

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
like:

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

<% end %>

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

classroom.teachers

Hope that helps.
Tom

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 http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Tom D.

http://blog.atomgiant.com
http://gifthat.com