Farrel
2009-Feb-12 00:56 UTC
Transaction block still sets id on model/new_record to false on rollback
I''m on Rails 2.2.2 on MySQL with InnoDB table types. I''m running into a wierd bug where if I have two models in a transaction and the second model raises an exception, the first model still has it''s ID set and it''s new_record status set to false despite the row the ID is set to never existing. The following is a console session showing this:>> client_user = new_client_user( :first_name => nil ) # Will cause validation to fail=> #<ClientUser id: nil, login: "rene173744", email_address: "farrel-tTrQVXjjHs3QT0dZR+AlfA@public.gmane.org", first_name: nil, last_name: "Sim", active: true>>> establishment = new_establishment( :brand => brand )=> #<Establishment id: nil, brand_id: 4, name: "Pretoria GrandWest", active: true>>> Establishment.transaction{ establishment.save!; client_user.save! } rescue false=> false>> establishment.id=> 10>> establishment.new_record?=> false>> client_user.id=> nil>> establishment.reloadActiveRecord::RecordNotFound: Couldn''t find Establishment with ID=10 <snip>>> Establishment.count=> 4 Here''s an explanation of the session: 1) I initialise a new client user, with first_name set to nil so it will fail a validation 2) I then initialise a new establishment model 3) I try and save both in a transaction block. The client_user.save! call raises an exception causing the block to roll back. 4) Yet establishment has an ID set to 10 and is marked as not being a new record. 5) As expected client_user has no ID 6) Calling reload on establishment throws an error because the row does not exist 7) Counting up the actual rows we see it is below the ID assigned to establishment (I did this a few times, the first time the assigned ID was 5, then 6, then 7 etc) Am I missing something or is this expected behaviour? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2009-Feb-13 09:12 UTC
Re: Transaction block still sets id on model/new_record to false on rollback
Farrel wrote:> I''m on Rails 2.2.2 on MySQL with InnoDB table types. I''m running into > a wierd bug where if I have two models in a transaction and the second > model raises an exception, the first model still has it''s ID set and > it''s new_record status set to false despite the row the ID is set to > never existing.Yes, that''s the way Transactions (now) work. The correct way to handle this is to not proceed with a transaction unless all model instances being saved in that transaction have been validated. The alternative is to wrap rollback_active_record_state! blocks around the transaction for all instances being saved except the last. -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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 -~----------~----~----~----~------~----~------~--~---