# Transaction error doesn't throw exception?

**URL:** <https://www.ruby-forum.com/t/transaction-error-doesnt-throw-exception/105172>\
**Category:** Rails\
**Created:** [May 26, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/transaction-error-doesnt-throw-exception/105172 "2007-05-26T17:21:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 26, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/transaction-error-doesnt-throw-exception/105172/1 "2007-05-26T17:21:53Z")

</div>

I need to save 3 kinds of records in one transaction and throw an  
exception if one error occurs in any one of the models : User -\>\>  
Proposal -\>\> Rating

I wrote in my controller :  
…  
user = User.new(…)

```
begin
  User.transaction(user) do
    user.save
    proposal = Proposal.new(:user_id => @user.id, ..... )
    proposal.save
    rating = Rating.new( :proposal => @proposal.id, .... )
    rating.save

  end
rescue
  render :action => 'error_action'
end

redirect_to :action => 'no_error-action'

```

end

I have errors in user, proposal and rating but no exception is raised …  
why ?

thanks

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 26, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/transaction-error-doesnt-throw-exception/105172/2 "2007-05-26T17:36:31Z")

</div>

I think that you need to change

user.save  
proposal.save  
rating.save

to

user.save!  
proposal.save!  
rating.save!

user.save returns either true or false, any errors will be found in  
user.errors

user.save! will raise an exception on failure.

- matt.

Kad K. wrote:

> I need to save 3 kinds of records in one transaction and throw an  
> exception if one error occurs in any one of the models : User -\>\>  
> Proposal -\>\> Rating
> 
> I wrote in my controller :  
> …  
> user = User.new(…)
> 
> ```
> begin
> User.transaction(user) do
> user.save
> proposal = Proposal.new(:user_id => @user.id, ..... )
> proposal.save
> rating = Rating.new( :proposal => @proposal.id, .... )
> rating.save
> 
> end
> rescue
> render :action => 'error_action'
> end
> 
> redirect_to :action => 'no_error-action'
> 
> ```
> 
> end
> 
> I have errors in user, proposal and rating but no exception is raised …  
> why ?
> 
> thanks
> 
> kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 26, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/transaction-error-doesnt-throw-exception/105172/3 "2007-05-26T18:36:06Z")

</div>

Thanks Mathieu… that’s it… the exception is raised (forgot the !)  
… so the rollback is done

but it’s a chicken and egg … as I need to display the errors  
I have to found how to get the errors…  
anyway to use the save and raise manually the exception ?

kad

Matthieu S. wrote:

> I think that you need to change
> 
> user.save  
> proposal.save  
> rating.save
> 
> to
> 
> user.save!  
> proposal.save!  
> rating.save!
> 
> user.save returns either true or false, any errors will be found in  
> user.errors
> 
> user.save! will raise an exception on failure.
> 
> - matt.
> 
> Kad K. wrote:
> 
> > I need to save 3 kinds of records in one transaction and throw an  
> > exception if one error occurs in any one of the models : User -\>\>  
> > Proposal -\>\> Rating
> > 
> > I wrote in my controller :  
> > …  
> > user = User.new(…)
> > 
> > ```
> > begin
> > User.transaction(user) do
> > user.save
> > proposal = Proposal.new(:user_id => @user.id, ..... )
> > proposal.save
> > rating = Rating.new( :proposal => @proposal.id, .... )
> > rating.save
> > 
> > end
> > rescue
> > render :action => 'error_action'
> > end
> > 
> > redirect_to :action => 'no_error-action'
> > 
> > ```
> > 
> > end
> > 
> > I have errors in user, proposal and rating but no exception is raised …  
> > why ?
> > 
> > thanks
> > 
> > kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 28, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/transaction-error-doesnt-throw-exception/105172/4 "2007-05-28T16:25:14Z")

</div>

Hi Kad,

This will trap most errors

begin  
do something  
rescue =\> e  
puts “Oops… #{e.message}”  
end

From memory, I think the model state is rolled back as well, so you  
won’t be able to interogate Model.errors to see exactly what went wrong.

rgds,

- matt.

Kad K. wrote:

> Thanks Mathieu… that’s it… the exception is raised (forgot the !)  
> … so the rollback is done
> 
> but it’s a chicken and egg … as I need to display the errors  
> I have to found how to get the errors…  
> anyway to use the save and raise manually the exception ?
> 
> kad
