# Views to handle a one-to-many relation?

**URL:** <https://www.ruby-forum.com/t/views-to-handle-a-one-to-many-relation/139188>\
**Category:** Rails\
**Created:** [June 1, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/views-to-handle-a-one-to-many-relation/139188 "2008-06-01T12:37:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrSSSSs\_MejSSS\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@AndrSSSSs\_MejSSS\_a](https://www.ruby-forum.com/u/AndrSSSSs_MejSSS_a)\
**Post date:** [June 1, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/views-to-handle-a-one-to-many-relation/139188/1 "2008-06-01T12:37:18Z")

</div>

Hello,  
I have a one-to-many relation between rooms and guests. A room has\_many  
guests, and a guest belongs\_to a single room.

I’ve got a basic view for this, where I can show a room with a list of  
all  
guests belonging to it. The problem is I can see these relations, but  
yet  
can’t add new guests to a room (The ones I see were associated via  
script/console).

I want to show a room with all its guests and a link that lets me add  
new  
guests to it. This link should take me to a list of all “roomless”  
guests,  
where I should be able to choose some of these guests and add them to a  
room.

I don’t know how to do this. Can you help me out?

Thanks a lot for your help.  
Sincerely,  
AndrÃ©s.

---

<div class="post-metadata">

**Author:** ![AndrSSSSs\_MejSSS\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@AndrSSSSs\_MejSSS\_a](https://www.ruby-forum.com/u/AndrSSSSs_MejSSS_a)\
**Post date:** [June 1, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/views-to-handle-a-one-to-many-relation/139188/2 "2008-06-01T17:37:23Z")

</div>

Hi Andres,

There are many ways to do this.

Essentially, if you’re in the rooms/show page, (say with room’s id is  
4 for the purposes of this example).

The url is /rooms/show/4

Now, the room is being shown in the view, with all its guests

The guests need to be links such as this:

\<%= link\_to guest.name, :controller =\> ‘guests’, :action =\>  
‘new’, :room\_id =\> @room.id %\>

Now in the new action of the guests controller, you’d have:

@guest = Guest.new(:room\_id =\> params[:room\_id])

and in the form for your guest, you’d have a hidden field

\<%= hidden\_field ‘guest’, ‘room\_id’ %\>

so that your guests/create action can take up the room\_id attribute  
when it is has

@guest = Guest.new(params[:guest])  
if(@guest.save)  
(etc.)

That’s one way to do it 🙂

If you want it with your roomless guests, then your new action should  
actually have something like:

@guests = Guest.find(params[:guest\_id])  
@roomless\_guests = Guest.find(:all, :conditions =\> “room\_id IS NULL”)

So that you’re not creating them, and then the links will link to some  
other action, which associates the id’s.

\<% for guest in @guests %\>  
\<%= link\_to ‘associate’, :action =\>  
‘associate\_guest\_to\_room’, :guest\_id =\> guest.id, :room\_id =\> @room.id  
%\>  
\<% end %\>

Hope that helps,  
Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #4 parts a and b now available!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![AndrSSSSs\_MejSSS\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@AndrSSSSs\_MejSSS\_a](https://www.ruby-forum.com/u/AndrSSSSs_MejSSS_a)\
**Post date:** [June 2, 2008, 1:14am UTC](https://www.ruby-forum.com/t/views-to-handle-a-one-to-many-relation/139188/3 "2008-06-02T01:14:07Z")

</div>

Thanks a lot for your help, Julian. It has been very useful for me 🙂

On Sun, Jun 1, 2008 at 10:35 AM, Julian L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
