xponrails
2008-Apr-11 10:25 UTC
How can I handle multiple models saves in a single transactional
Hi all. I need to create two model in a single transaction: if the first fails, the transaction should be rolled back; if the second fails, the transaction should be rolled back. This is my code: 1. begin ActiveRecord::Base.transaction do 2. @first_model = FirstModel.new(params[:first_model]) 3. @first_model.save! 4. @second_model = SecondModel.new(:user => current_user, :first_model => @first_model) 5. @second_model.save! 6. end 7. rescue Exception => @ex 8. # handle the exception 9. end The global rollback works only if the "@first_model.save!" fails. If the "@second_model.save!" fails, the global transaction isn''t rolled back, and the first_model is still saved on database. I''ve also tried using nested transactions: 1. begin 2. FirstModel.transaction do 3. SecondModel.transaction do 4. @first_model = FirstModel.new(params[:first_model]) 5. @first_model.save! 6. @second_model = SecondModel.new(:user => current_user, :first_model => @first_model) 7. @second_model.save! 8. end 9. end 10.rescue Exception => @ex 11. # handle the exception 12.end but also this solutions shows the same problems as above. How can I solve my issue? Is there a way to define a sort of global transaction in Rails? Thanks, Stefano --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-11 10:32 UTC
Re: How can I handle multiple models saves in a single transactional
On 11 Apr 2008, at 11:25, xponrails wrote:> > Hi all. > > I need to create two model in a single transaction: if the first > fails, the transaction should be rolled back; if the second fails, the > transaction should be rolled back. > > This is my code: > > 1. begin ActiveRecord::Base.transaction do > 2. @first_model = FirstModel.new(params[:first_model]) > 3. @first_model.save! > 4. @second_model = SecondModel.new(:user => > current_user, :first_model => @first_model) > 5. @second_model.save! > 6. end > 7. rescue Exception => @ex > 8. # handle the exception > 9. end >> The global rollback works only if the "@first_model.save!" fails. > If the "@second_model.save!" fails, the global transaction isn''t > rolled back, and the first_model is still saved on database. >Are testing this from a unit test or in development mode ?> I''ve also tried using nested transactions:(rails doesn''t support nested transactions. the second and following transactions are basically no-ops) Fred>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
xponrails
2008-Apr-11 11:40 UTC
Re: How can I handle multiple models saves in a single transactional
I''ve tested this in development mode, breaking the second save! using: @second_model = SecondModel.new(:user => "mickeymouse", :first_model => @first_model) @second_model.save! This code throws an exception due to the string "mickeymouse" and this second save is rolled back. But the first save isn''t rolled back and the @first_model is saved in my database. PS: I''m using MySql 5 and Rails 2.0.2 On 11 Apr, 12:32, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 11 Apr 2008, at 11:25, xponrails wrote: > > > > > > > Hi all. > > > I need to create two model in a single transaction: if the first > > fails, the transaction should be rolled back; if the second fails, the > > transaction should be rolled back. > > > This is my code: > > > 1. begin ActiveRecord::Base.transaction do > > 2. @first_model = FirstModel.new(params[:first_model]) > > 3. @first_model.save! > > 4. @second_model = SecondModel.new(:user => > > current_user, :first_model => @first_model) > > 5. @second_model.save! > > 6. end > > 7. rescue Exception => @ex > > 8. # handle the exception > > 9. end > > > The global rollback works only if the "@first_model.save!" fails. > > If the "@second_model.save!" fails, the global transaction isn''t > > rolled back, and the first_model is still saved on database. > > Are testing this from a unit test or in development mode ?> I''ve also tried using nested transactions: > > (rails doesn''t support nested transactions. the second and following > transactions are basically no-ops) > > Fred > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-11 12:33 UTC
Re: How can I handle multiple models saves in a single transactional
On 11 Apr 2008, at 12:40, xponrails wrote:> > I''ve tested this in development mode, breaking the second save! using: > > @second_model = SecondModel.new(:user => "mickeymouse", :first_model > => @first_model) > @second_model.save! > > This code throws an exception due to the string "mickeymouse" and this > second save is rolled back. But the first save isn''t rolled back and > the @first_model is saved in my database. > PS: I''m using MySql 5 and Rails 2.0.2 >That''s odd. I suspect what is happening (check this in your logs) is that the first save is being wrapped in a transaction, but since you already have a transaction going, starting that transaction implicitly commits the first. However rails should notice that you are already in a transaction and not do this. Fred> > > > On 11 Apr, 12:32, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 11 Apr 2008, at 11:25, xponrails wrote: >> >> >> >> >> >>> Hi all. >> >>> I need to create two model in a single transaction: if the first >>> fails, the transaction should be rolled back; if the second fails, >>> the >>> transaction should be rolled back. >> >>> This is my code: >> >>> 1. begin ActiveRecord::Base.transaction do >>> 2. @first_model = FirstModel.new(params[:first_model]) >>> 3. @first_model.save! >>> 4. @second_model = SecondModel.new(:user => >>> current_user, :first_model => @first_model) >>> 5. @second_model.save! >>> 6. end >>> 7. rescue Exception => @ex >>> 8. # handle the exception >>> 9. end >> >>> The global rollback works only if the "@first_model.save!" fails. >>> If the "@second_model.save!" fails, the global transaction isn''t >>> rolled back, and the first_model is still saved on database. >> >> Are testing this from a unit test or in development mode ?> I''ve >> also tried using nested transactions: >> >> (rails doesn''t support nested transactions. the second and following >> transactions are basically no-ops) >> >> Fred >> >> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
xponrails
2008-Apr-11 13:52 UTC
Re: How can I handle multiple models saves in a single transactional
this is the except of my log: ||[4;36;1mSQL (0.000000)||[0m ||[0;1mBEGIN||[0m ||[4;35;1mFirstModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM `fisrt_models`||[0m ||[4;36;1mFirstModel Create (0.000000)||[0m ||[0;1mINSERT INTO `first_models` (`name`) VALUES(''tsdf'')||[0m ||[4;35;1mSecondModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM `second_models`||[0m ||[4;36;1mSQL (0.000000)||[0m ||[0;1mROLLBACK||[0m User expected, got String On 11 Apr, 14:33, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 11 Apr 2008, at 12:40, xponrails wrote: > > > > > I''ve tested this in development mode, breaking the second save! using: > > > @second_model = SecondModel.new(:user => "mickeymouse", :first_model > > => @first_model) > > @second_model.save! > > > This code throws an exception due to the string "mickeymouse" and this > > second save is rolled back. But the first save isn''t rolled back and > > the @first_model is saved in my database. > > PS: I''m using MySql 5 and Rails 2.0.2 > > That''s odd. I suspect what is happening (check this in your logs) is > that the first save is being wrapped in a transaction, but since you > already have a transaction going, starting that transaction implicitly > commits the first. However rails should notice that you are already in > a transaction and not do this. > > Fred > > > > > On 11 Apr, 12:32, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> On 11 Apr 2008, at 11:25, xponrails wrote: > > >>> Hi all. > > >>> I need to create two model in a single transaction: if the first > >>> fails, the transaction should be rolled back; if the second fails, > >>> the > >>> transaction should be rolled back. > > >>> This is my code: > > >>> 1. begin ActiveRecord::Base.transaction do > >>> 2. @first_model = FirstModel.new(params[:first_model]) > >>> 3. @first_model.save! > >>> 4. @second_model = SecondModel.new(:user => > >>> current_user, :first_model => @first_model) > >>> 5. @second_model.save! > >>> 6. end > >>> 7. rescue Exception => @ex > >>> 8. # handle the exception > >>> 9. end > > >>> The global rollback works only if the "@first_model.save!" fails. > >>> If the "@second_model.save!" fails, the global transaction isn''t > >>> rolled back, and the first_model is still saved on database. > > >> Are testing this from a unit test or in development mode ?> I''ve > >> also tried using nested transactions: > > >> (rails doesn''t support nested transactions. the second and following > >> transactions are basically no-ops) > > >> Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ruby Freak
2008-Apr-11 14:13 UTC
Re: How can I handle multiple models saves in a single transactional
I am no genus, but a couple things come to mind. It appears that save! throws an exception upon failure and is supposed to trigger a rollback. You are rescuing that exception. Maybe try it without the rescue. The transaction is only on the @first_model which it appears is not cascading to the @second_model Maybe instead of save!, use the save message which returns false instead of raising an exception. @second_model = SecondModel.new(:user => "mickeymouse", :first_model => @first_model) @first_model.rollback unless @second_model.save Good luck On Apr 11, 6:52 am, xponrails <stefac...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> this is the except of my log: > > ||[4;36;1mSQL (0.000000)||[0m ||[0;1mBEGIN||[0m > ||[4;35;1mFirstModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM > `fisrt_models`||[0m > ||[4;36;1mFirstModel Create (0.000000)||[0m ||[0;1mINSERT INTO > `first_models` (`name`) VALUES(''tsdf'')||[0m > ||[4;35;1mSecondModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM > `second_models`||[0m > ||[4;36;1mSQL (0.000000)||[0m ||[0;1mROLLBACK||[0m > User expected, got String > > On 11 Apr, 14:33, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 11 Apr 2008, at 12:40, xponrails wrote: > > > > I''ve tested this in development mode, breaking the second save! using: > > > > @second_model = SecondModel.new(:user => "mickeymouse", :first_model > > > => @first_model) > > > @second_model.save! > > > > This code throws an exception due to the string "mickeymouse" and this > > > second save is rolled back. But the first save isn''t rolled back and > > > the @first_model is saved in my database. > > > PS: I''m using MySql 5 and Rails 2.0.2 > > > That''s odd. I suspect what is happening (check this in your logs) is > > that the first save is being wrapped in a transaction, but since you > > already have a transaction going, starting that transaction implicitly > > commits the first. However rails should notice that you are already in > > a transaction and not do this. > > > Fred > > > > On 11 Apr, 12:32, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> On 11 Apr 2008, at 11:25, xponrails wrote: > > > >>> Hi all. > > > >>> I need to create two model in a single transaction: if the first > > >>> fails, the transaction should be rolled back; if the second fails, > > >>> the > > >>> transaction should be rolled back. > > > >>> This is my code: > > > >>> 1. begin ActiveRecord::Base.transaction do > > >>> 2. @first_model = FirstModel.new(params[:first_model]) > > >>> 3. @first_model.save! > > >>> 4. @second_model = SecondModel.new(:user => > > >>> current_user, :first_model => @first_model) > > >>> 5. @second_model.save! > > >>> 6. end > > >>> 7. rescue Exception => @ex > > >>> 8. # handle the exception > > >>> 9. end > > > >>> The global rollback works only if the "@first_model.save!" fails. > > >>> If the "@second_model.save!" fails, the global transaction isn''t > > >>> rolled back, and the first_model is still saved on database. > > > >> Are testing this from a unit test or in development mode ?> I''ve > > >> also tried using nested transactions: > > > >> (rails doesn''t support nested transactions. the second and following > > >> transactions are basically no-ops) > > > >> Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ruby Freak
2008-Apr-11 15:03 UTC
Re: How can I handle multiple models saves in a single transactional
I found this Wes Gamble From: Wes Gamble <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> Date: Fri, 19 Jan 2007 18:54:09 +0100 Local: Fri, Jan 19 2007 10:54 am Subject: AR transaction method doesn''t rollback in all cases All, I think I''m seeing the behavior specified in this post to Rails-core: http://www.ruby-forum.com/topic/81605 I believe that this post is an example of the behavior I''ll describe as well: http://www.ruby-forum.com/topic/83260 I''d like to verify the following assertion: Assertion: Within a transaction block, a rollback will not occur automatically unless an exception is thrown from within that top-level block explicitly. If there are begin/rescue blocks within the transaction block that catch exceptions, then the transaction block will not be able to detect these exceptions and will not rollback. I''m seeing this on a multi-object update, so I am now using save (instead of save!) and rollback_db_transaction explicitly by detecting whether each object''s error array is non-empty. Thanks, Wes -- Posted via http://www.ruby-forum.com/. On Apr 11, 7:13 am, Ruby Freak <twscann...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am no genus, but a couple things come to mind. > > It appears that save! throws an exception upon failure and is > supposed to trigger arollback. You are rescuing that exception. > Maybe try it without the rescue. > > The transaction is only on the @first_model which it appears is not > cascading to the @second_model > Maybe instead of save!, use the save message which returns false > instead of raising an exception. > > @second_model = SecondModel.new(:user => "mickeymouse", :first_model > => @first_model) > @first_model.rollbackunless @second_model.save > > Good luck > > On Apr 11, 6:52 am, xponrails <stefac...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > this is the except of my log: > > > ||[4;36;1mSQL (0.000000)||[0m ||[0;1mBEGIN||[0m > > ||[4;35;1mFirstModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM > > `fisrt_models`||[0m > > ||[4;36;1mFirstModel Create (0.000000)||[0m ||[0;1mINSERT INTO > > `first_models` (`name`) VALUES(''tsdf'')||[0m > > ||[4;35;1mSecondModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM > > `second_models`||[0m > > ||[4;36;1mSQL (0.000000)||[0m ||[0;1mROLLBACK||[0m > > User expected, got String > > > On 11 Apr, 14:33, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On 11 Apr 2008, at 12:40, xponrails wrote: > > > > > I''ve tested this in development mode, breaking the second save! using: > > > > > @second_model = SecondModel.new(:user => "mickeymouse", :first_model > > > > => @first_model) > > > > @second_model.save! > > > > > This code throws an exception due to the string "mickeymouse" and this > > > > second save is rolled back. But the first save isn''t rolled back and > > > > the @first_model is saved in my database. > > > > PS: I''m using MySql 5 and Rails 2.0.2 > > > > That''s odd. I suspect what is happening (check this in your logs) is > > > that the first save is being wrapped in a transaction, but since you > > > already have a transaction going, starting that transaction implicitly > > > commits the first. However rails should notice that you are already in > > > a transaction and not do this. > > > > Fred > > > > > On 11 Apr, 12:32, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >> On 11 Apr 2008, at 11:25, xponrails wrote: > > > > >>> Hi all. > > > > >>> I need to create two model in a single transaction: if the first > > > >>> fails, the transaction should be rolled back; if the second fails, > > > >>> the > > > >>> transaction should be rolled back. > > > > >>> This is my code: > > > > >>> 1. begin ActiveRecord::Base.transaction do > > > >>> 2. @first_model = FirstModel.new(params[:first_model]) > > > >>> 3. @first_model.save! > > > >>> 4. @second_model = SecondModel.new(:user => > > > >>> current_user, :first_model => @first_model) > > > >>> 5. @second_model.save! > > > >>> 6. end > > > >>> 7. rescue Exception => @ex > > > >>> 8. # handle the exception > > > >>> 9. end > > > > >>> The globalrollbackworks only if the "@first_model.save!" fails. > > > >>> If the "@second_model.save!" fails, the global transaction isn''t > > > >>> rolled back, and the first_model is still saved on database. > > > > >> Are testing this from a unit test or in development mode ?> I''ve > > > >> also tried using nested transactions: > > > > >> (rails doesn''t support nested transactions. the second and following > > > >> transactions are basically no-ops) > > > > >> Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
xponrails
2008-Apr-11 15:42 UTC
Re: How can I handle multiple models saves in a single transactional
Hi. For now I''ve resolved using this code: begin @first_model = FirstModel.new(params[:first_model]) @second_model = SecondModel.new(:user => current_user, :first_model => @first_model) if @first_model.save and @second_model.save logger.warn "saved" else logger.warn "this should rollback" end rescue Exception => e end I''ve placed in a single if statement all the saves "if @first_model.save and @second_model.save" and all works, but I really don''t like this approach. And I don''t understand WHY Rails and ActiveRecords cannot let the developer to handle complex transactions by himself. On 11 Apr, 17:03, Ruby Freak <twscann...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I found this > > Wes Gamble > From: Wes Gamble <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > Date: Fri, 19 Jan 2007 18:54:09 +0100 > Local: Fri, Jan 19 2007 10:54 am > Subject: AR transaction method doesn''t rollback in all cases > > All, > > I think I''m seeing the behavior specified in this post to Rails-core:http://www.ruby-forum.com/topic/81605 > > I believe that this post is an example of the behavior I''ll describe > as > well:http://www.ruby-forum.com/topic/83260 > > I''d like to verify the following assertion: > > Assertion: Within a transaction block, a rollback will not occur > automatically unless an exception is thrown from within that top-level > block explicitly. If there are begin/rescue blocks within the > transaction block that catch exceptions, then the transaction block > will > not be able to detect these exceptions and will not rollback. > > I''m seeing this on a multi-object update, so I am now using save > (instead of save!) and rollback_db_transaction explicitly by detecting > whether each object''s error array is non-empty. > > Thanks, > Wes > > -- > Posted viahttp://www.ruby-forum.com/. > > On Apr 11, 7:13 am, Ruby Freak <twscann...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am no genus, but a couple things come to mind. > > > It appears that save! throws an exception upon failure and is > > supposed to trigger arollback. You are rescuing that exception. > > Maybe try it without the rescue. > > > The transaction is only on the @first_model which it appears is not > > cascading to the @second_model > > Maybe instead of save!, use the save message which returns false > > instead of raising an exception. > > > @second_model = SecondModel.new(:user => "mickeymouse", :first_model > > => @first_model) > > @first_model.rollbackunless @second_model.save > > > Good luck > > > On Apr 11, 6:52 am, xponrails <stefac...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > this is the except of my log: > > > > ||[4;36;1mSQL (0.000000)||[0m ||[0;1mBEGIN||[0m > > > ||[4;35;1mFirstModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM > > > `fisrt_models`||[0m > > > ||[4;36;1mFirstModel Create (0.000000)||[0m ||[0;1mINSERT INTO > > > `first_models` (`name`) VALUES(''tsdf'')||[0m > > > ||[4;35;1mSecondModel Columns (0.000000)||[0m ||[0mSHOW FIELDS FROM > > > `second_models`||[0m > > > ||[4;36;1mSQL (0.000000)||[0m ||[0;1mROLLBACK||[0m > > > User expected, got String > > > > On 11 Apr, 14:33, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On 11 Apr 2008, at 12:40, xponrails wrote: > > > > > > I''ve tested this in development mode, breaking the second save! using: > > > > > > @second_model = SecondModel.new(:user => "mickeymouse", :first_model > > > > > => @first_model) > > > > > @second_model.save! > > > > > > This code throws an exception due to the string "mickeymouse" and this > > > > > second save is rolled back. But the first save isn''t rolled back and > > > > > the @first_model is saved in my database. > > > > > PS: I''m using MySql 5 and Rails 2.0.2 > > > > > That''s odd. I suspect what is happening (check this in your logs) is > > > > that the first save is being wrapped in a transaction, but since you > > > > already have a transaction going, starting that transaction implicitly > > > > commits the first. However rails should notice that you are already in > > > > a transaction and not do this. > > > > > Fred > > > > > > On 11 Apr, 12:32, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > >> On 11 Apr 2008, at 11:25, xponrails wrote: > > > > > >>> Hi all. > > > > > >>> I need to create two model in a single transaction: if the first > > > > >>> fails, the transaction should be rolled back; if the second fails, > > > > >>> the > > > > >>> transaction should be rolled back. > > > > > >>> This is my code: > > > > > >>> 1. begin ActiveRecord::Base.transaction do > > > > >>> 2. @first_model = FirstModel.new(params[:first_model]) > > > > >>> 3. @first_model.save! > > > > >>> 4. @second_model = SecondModel.new(:user => > > > > >>> current_user, :first_model => @first_model) > > > > >>> 5. @second_model.save! > > > > >>> 6. end > > > > >>> 7. rescue Exception => @ex > > > > >>> 8. # handle the exception > > > > >>> 9. end > > > > > >>> The globalrollbackworks only if the "@first_model.save!" fails. > > > > >>> If the "@second_model.save!" fails, the global transaction isn''t > > > > >>> rolled back, and the first_model is still saved on database. > > > > > >> Are testing this from a unit test or in development mode ?> I''ve > > > > >> also tried using nested transactions: > > > > > >> (rails doesn''t support nested transactions. the second and following > > > > >> transactions are basically no-ops) > > > > > >> Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---