Forum: Ruby on Rails Best way about doing this?

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.
7b670ec0cdba007e3c08c7cc14ec57f7?d=identicon&s=25 Benson Lott (lammmy)
on 2006-03-27 02: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])
	end
22580f640e491f0920a192610d1df393?d=identicon&s=25 Tom Davies (Guest)
on 2006-03-27 02: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:
http://api.rubyonrails.org/classes/ActiveRecord/As...


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 Lott <bensonlott@gmail.com> 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Tom Davies

http://blog.atomgiant.com
http://gifthat.com
This topic is locked and can not be replied to.