Is it time for transactions yet?

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…

Am Dienstag, den 29.11.2005, 18:45 -0800 schrieb subimage interactive:

over it real quickly and tell me what I’m doing wrong? I’m only
:order])
# In that case, it needs to be present to display for the “new”
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

If you use .save! an exception is thrown instead of returning false.
Your else block will never be called.

Anyway, if you associate some new records and save the parent, the child
record will also be saved. But if the child record is not valid, the
parent record would be saved, but not the child record, though the .save
method returns true. Have it?

For answering your question: Yes, transactions might be a way.

Seth,

If you are trying to create associations you don’t want to call new.
@order = @order_user.orders.new(params[:order])

Use build to create a new order object, add it to the orders
collection and not save it immediately:
@order = @order_user.orders.build(params[:order])

Check out the docs on associations, too:
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html

Cody

On 11/30/05, Norman T. [email protected] wrote:

my records they all would be saved at the same time. Can anyone look
@order = @order_user.orders.new(params[
# Need to re-create it here in case save fails.
flash[:notice] = ‘Order was successfully created.’
record will also be saved. But if the child record is not valid, the
http://www.subimage.com/sublog/


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


http://www.codyfauser.com

Ahhh…build looks good. Thanks for that. I wasn’t aware that a call
like
that even existed. I tried wrapping everything inside a transaction, but
there were problems with the associations not being created as well. I’m
guessing it had to do with the records not being saved (thus the id’s
not
being generated).

This is what I ended up doing so far, and it’s working - but not too
pretty.
There’s probably a better ruby way to go about things. I’m guessing I
should
replace my creates with build, then save! so my exception code can be
called
if there are any problems.

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

begin
  @order_user.save!
  # Create order
  @order = @order_user.orders.create(params[:order])
  @order.order_number = Order.generate_order_number
  @order.date = Time.now
  @order.save!
  # Addresses
  @billing_address =

@order_user.order_addresses.create(params[:billing_address])
if @use_separate_shipping_address then
@shipping_address =
@order_user.order_addresses.create(params[:shipping_address])
end
@order_account = OrderAccount.new(params[:order_account])
@order_account.order_user_id = @order_user.id
@order_account.order_address_id = @billing_address.id
@order_account.save!
# All went well?
flash[:notice] = ‘Order was successfully created.’
redirect_to :action => ‘edit’, :id => @order.id
rescue
# If anything fails remove everything from the database.
# We don’t want any orphan records floating around.
#
# All commits need to happen so we don’t have fucked up logic
# to see if some things were written to the db, some not, etc.
@order_user.destroy if @order_user
@order_user = OrderUser.new(params[:order_user])

  @order.destroy if @order
  @order = Order.new

  @billing_address.destroy if @billing_address
  @billing_address = OrderAddress.new(params[:billing_address])

  @shipping_address.destroy if @shipping_address
  @shipping_address = OrderAddress.new(params[:shipping_address])

  @order_account.destroy if @order_account
  @order_account = OrderAccount.new(params[:order_account])
  render :action => 'new'
end

end