Forum: Ruby on Rails has_one question

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.
66d1d0df01e4bbb82674a353ea68d6ee?d=identicon&s=25 Josh Kieschnick (jjkiesch)
on 2007-08-01 16:00
(Received via mailing list)
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?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-08-01 16:50
(Received via mailing list)
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.
This topic is locked and can not be replied to.