# Simple one-to-one relationship question

**URL:** <https://www.ruby-forum.com/t/simple-one-to-one-relationship-question/51380>\
**Category:** Rails\
**Created:** [December 30, 2005, 3:26pm UTC](https://www.ruby-forum.com/t/simple-one-to-one-relationship-question/51380 "2005-12-30T15:26:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [December 30, 2005, 3:26pm UTC](https://www.ruby-forum.com/t/simple-one-to-one-relationship-question/51380/1 "2005-12-30T15:26:41Z")

</div>

Hi all,

In my shop application I have created a form (rhtml file) containing  
both  
address and order information.

I created my fields: like the following:

For address info: \<%= text\_field ‘address’, ‘postalcode’ %\>  
For order info: \<%= text\_field ‘order’, ‘email’ %\>

However when the following action gets submitted, my order object gets  
saved  
without any problems, but it never saves my Address object.  
I would expect that the statement @order.save would also save my Address  
object.

def checkout\_shipto  
if request.get?  
@order = Order.new  
@address = Address.new  
@order.address = @address  
else  
@order = Order.new(params[:order])  
@address = Address.new(params[:address])  
@order.address = @address  
if @order.save  
redirect\_to(:action =\> “checkout\_summary”)  
else  
render\_action(‘checkout\_shipto’)  
end  
end  
end

I have defined my model objects as follows:

class Order \< ActiveRecord::Base  
has\_many :line\_items  
has\_one :address, :dependent=\>true  
end

class Address \< ActiveRecord::Base  
belongs\_to :order  
validates\_presence\_of :street, :postalcode, :city, :country,  
:address  
end

Also, no validation on the Address object takes place. I think this is  
because it never gets saved though…

Can you please help?

Regards,

Harm de Laat

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [January 2, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/simple-one-to-one-relationship-question/51380/2 "2006-01-02T19:13:03Z")

</div>

Instead of using:

```
      @order.address = @address

```

try

```
      @order.address << @address

```

The \<\< appends the new address record and sets the foreign keys  
properly.
