# Transaction Failures

**URL:** <https://discuss.rubyonrails.org/t/transaction-failures/51714>\
**Category:** rubyonrails-talk\
**Created:** [May 18, 2010, 8:24am UTC](https://discuss.rubyonrails.org/t/transaction-failures/51714 "2010-05-18T08:24:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@brianp](https://discuss.rubyonrails.org/u/brianp)\
**Post date:** [May 18, 2010, 8:24am UTC](https://discuss.rubyonrails.org/t/transaction-failures/51714/1 "2010-05-18T08:24:17Z")

</div>

Hey,

I've got a multiple model (4) form on my site.

I have the controller handling the creation with a transaction. I do this because if one object can't save I don't want any of them saved and need to be able to quit the entire transaction without making any permanent db calls.

The problem I find is. If a validation fails on an object without the transaction present. The page refreshes with rails generated validation errors. With the transaction present I don't know how to send it back to the page with errors present. But again without the transaction if there is a problem all good objects get saved to the db and the bad one does not.

So the problem is: If the transaction fails. I need the form page to load with validation errors.

I use the code below:

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ActiveRecord::Base.transaction do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dealer = Dealer.new(params[:dealer]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dealer.save! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user = @dealer.users.build(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.role\_id = 3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dealer.addresses.build(params[:mailing\_address]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#@dealer.addresses.create(params[:billing\_address]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;rescue ActiveRecord::RecordInvalid =\> invalid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Vehicle was not created" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dealer = Dealer.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user = User.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action =\> "new" &nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;if ! @dealer.new\_record? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Dealer, User and addresses saved!" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_back\_or\_default login\_path &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

cheers.

---

<div class="post-metadata">

**Author:** ![brianp](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@brianp](https://discuss.rubyonrails.org/u/brianp)\
**Post date:** [May 18, 2010, 10:14am UTC](https://discuss.rubyonrails.org/t/transaction-failures/51714/2 "2010-05-18T10:14:27Z")

</div>

Found my problem immediately after posting.

In:

rescue ActiveRecord::RecordInvalid =\> invalid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Vehicle was not created" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dealer = Dealer.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user = User.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action =\> "new" end

Should be:

rescue ActiveRecord::RecordInvalid =\> invalid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = "Vehicle was not created" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action =\> "new" end

Creating new objects seems to get blank all the fields. As well I was missing separate \<%= f.error\_messages %\> for each model type.
