# Is it time for transactions yet?

**URL:** <https://www.ruby-forum.com/t/is-it-time-for-transactions-yet/80189>\
**Category:** Rails\
**Created:** [November 30, 2005, 3:48am UTC](https://www.ruby-forum.com/t/is-it-time-for-transactions-yet/80189 "2005-11-30T03:48:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [November 30, 2005, 3:48am UTC](https://www.ruby-forum.com/t/is-it-time-for-transactions-yet/80189/1 "2005-11-30T03:48:58Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [November 30, 2005, 12:10pm UTC](https://www.ruby-forum.com/t/is-it-time-for-transactions-yet/80189/2 "2005-11-30T12:10:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [November 30, 2005, 4:09pm UTC](https://www.ruby-forum.com/t/is-it-time-for-transactions-yet/80189/3 "2005-11-30T16:09:37Z")

</div>

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](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)

Cody

On 11/30/05, Norman T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.subimage.com/sublog/)
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
[http://www.codyfauser.com](http://www.codyfauser.com)

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [December 1, 2005, 5:47am UTC](https://www.ruby-forum.com/t/is-it-time-for-transactions-yet/80189/4 "2005-12-01T05:47:37Z")

</div>

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
