views:

23

answers:

2

I have the following create action:

def create
    @order = Order.new(params[:order])

    if params[:same_as_above] == "1"
      @order.billing_address.name = @order.shipping_address.name
      @order.billing_address.number = @order.shipping_address.number
      @order.billing_address.street = @order.shipping_address.town
    end

    if @order.save
      if @order.purchase
        render :action => "success"
      else
        render :action => "failure"
      end
    else
      render :action => 'new'
    end
  end

It works, but seems a bit cumbersome and brittle in the way i copy the shipping address to the billing address, attribute by attribute. Is there a better way please?

A: 

If your models are set up correctly, you can use:

@order.billing_address = @order.shipping_address
Justice
ActiveRecord::AssociationTypeMismatch in OrdersController#createBillingAddress(#18825000) expected, got ShippingAddress(#18874550)
pingu
That's essentially what I meant by models not being set up correctly.... Why are there two sets of classes and tables for `Address` - why both `ShippingAddress` and `BillingAddress`?
Justice
Because one tables data will be kept, while the other will be cleared on a regular basis. Thats why i have the separation.
pingu
A: 
@order.billing_address.attributes = @order.shipping_address.attributes

does the trick.

pingu