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.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-05-26 17: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
F0dd48b0d65fb23e2a3a4d22bde87705?d=identicon&s=25 Matthieu Stone (Guest)
on 2007-05-26 17: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 Kerforn 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
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-05-26 18: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 Stone 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 Kerforn 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
F0dd48b0d65fb23e2a3a4d22bde87705?d=identicon&s=25 Matt Stone (Guest)
on 2007-05-28 16: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 Kerforn 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.