Is it time for transactions yet?


#1

I’ve started on my second rails project, one which is a little more
complex
than my first.

In this, I’ve designed a DB to handle orders from an online store. My
orders
are comprised of records that span across 4 tables or so. I’ve not used
transactions to this point in rails, and I’m wondering if now is the
time to
start.

I was under the impression that if I used the syntax below to create my
records they all would be saved at the same time. Can anyone look over
it
real quickly and tell me what I’m doing wrong? I’m only getting
@order_user
saved, when I thought all the associated records should be persisted as
well…

Here’s the code:

Creates an order from new.

def create
@order_user = OrderUser.new(params[:order_user])

@order = @order_user.orders.new(params[:order])

@billing_address =

@order_user.order_addresses.new(params[:billing_address])

if params[:use_separate_shipping_address] then
  @shipping_address =

@order_user.order_addresses.new(params[:shipping_address])
else
# Don’t create shipping address as a child of order_user
# Need to re-create it here in case save fails.
# In that case, it needs to be present to display for the “new”
view
@shipping_address = OrderAddress.new(params[:shipping_address])
end

@order_account = 

@order_user.order_accounts.new(params[:order_account])

# Transaction support here?
if @order_user.save!
  flash[:notice] = 'Order was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end

end

As always, many thanks in advance…