# NOOB: Representing linked objects in one form

**URL:** <https://www.ruby-forum.com/t/noob-representing-linked-objects-in-one-form/60785>\
**Category:** Rails\
**Created:** [May 17, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/noob-representing-linked-objects-in-one-form/60785 "2006-05-17T14:57:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![donncha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90db22/32.png) [@donncha](https://www.ruby-forum.com/u/donncha)\
**Post date:** [May 17, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/noob-representing-linked-objects-in-one-form/60785/1 "2006-05-17T14:57:38Z")

</div>

Given two models:

User  
:name  
:email  
:address\_id # foreign key

Address  
:line\_1  
:line\_2  
:city

etc.

I want to have a form allowing a user to register, in which she’d enter  
an address as well, but how do I go about combining both objects into  
the one form?

I’m new to this and following along with the Agile Rails book from the  
Pragmatic Programmers, but it doesn’t mention this and I’m having  
trouble getting a decent answer with searches.

---

<div class="post-metadata">

**Author:** ![donncha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90db22/32.png) [@donncha](https://www.ruby-forum.com/u/donncha)\
**Post date:** [May 17, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/noob-representing-linked-objects-in-one-form/60785/2 "2006-05-17T15:40:37Z")

</div>

have a look over there : [www.ajaxscaffold.com](http://www.ajaxscaffold.com)

Thibaut

---

<div class="post-metadata">

**Author:** ![donncha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90db22/32.png) [@donncha](https://www.ruby-forum.com/u/donncha)\
**Post date:** [May 17, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/noob-representing-linked-objects-in-one-form/60785/3 "2006-05-17T15:56:21Z")

</div>

I’m not sure that it’s ajax that’s needed here as such.

The address fields on the form will look something like this:

You should then be able to create a new address via the user object in  
your controller:

def register\_user  
user = User.new(params[:user])  
if user.save  
user.addresses.create(params[:address])  
end  
end

I might be wrong but i would think that you need to have a user\_id in  
‘addresses’ rather than an address\_id in ‘users’ - the above assumes  
that this is the case. It also assumes that users have many addresses.

Hope this helps,

Steve

---

<div class="post-metadata">

**Author:** ![donncha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90db22/32.png) [@donncha](https://www.ruby-forum.com/u/donncha)\
**Post date:** [May 17, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/noob-representing-linked-objects-in-one-form/60785/4 "2006-05-17T18:01:54Z")

</div>

To keep with the Rails style, the text fields should look like this:

\<%= text\_field ‘address’, ‘line\_1’ %\>  
\<%= text\_field ‘address’, ‘line\_2’ %\>  
\<%= text\_field ‘address’, ‘city’ %\>

and Steve is right: if a user has\_one or has\_many addresses, the foriegn  
id field should be in the address table.

-Adam

---

<div class="post-metadata">

**Author:** ![donncha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90db22/32.png) [@donncha](https://www.ruby-forum.com/u/donncha)\
**Post date:** [May 18, 2006, 1:47am UTC](https://www.ruby-forum.com/t/noob-representing-linked-objects-in-one-form/60785/5 "2006-05-18T01:47:03Z")

</div>

Oops. Looks like I’ve more fundamental problems to be worrrying about.  
Thanks!
