Transaction error doesn't throw exception?


#1

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


#2

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


#3

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


#4

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