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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---