Forum: Ruby on Rails Transaction error doesn't throw exception ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kad K. (Guest)
on 2007-05-26 19:21
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
Matthieu S. (Guest)
on 2007-05-26 19:36
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
Kad K. (Guest)
on 2007-05-26 20:36
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
Matt S. (Guest)
on 2007-05-28 18:25
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
>
This topic is locked and can not be replied to.