# Has\_one question

**URL:** <https://www.ruby-forum.com/t/has-one-question/110969>\
**Category:** Rails\
**Created:** [August 1, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/has-one-question/110969 "2007-08-01T16:00:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 1, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/has-one-question/110969/1 "2007-08-01T16:00:33Z")

</div>

i have my orders and addresses in separate tables. in the Order model  
file i have has\_one :address

i have an address form that when submitted the first time, will create  
an address for the order, but if it needs to be submitted again, it  
should update the existing address instead of creating a new one.

in my controller i have this piece of code:

if @order.has\_address?  
@order.address.update\_attributes(params[:address])  
else  
@order.create\_address(params[:address])  
end

just seems like there would be a more elegant way of doing this… or  
am i being too picky?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 1, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/has-one-question/110969/2 "2007-08-01T16:50:41Z")

</div>

Josh wrote:

> @order.address.update\_attributes(params[:address])  
> else  
> @order.create\_address(params[:address])  
> end
> 
> just seems like there would be a more elegant way of doing this… or  
> am i being too picky?

Perhaps:

@order.build\_address unless @order.address  
@order.address.update\_attributes(params[:address])

–  
We develop, watch us RoR, in numbers too big to ignore.
