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:
“[email protected]”, 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
=> falseestablishment.id
=> 10establishment.new_record?
=> falseclient_user.id
=> nilestablishment.reload
ActiveRecord::RecordNotFound: Couldn’t find Establishment with ID=10
Establishment.count
=> 4
Here’s an explanation of the session:
- I initialise a new client user, with first_name set to nil so it
will fail a validation - I then initialise a new establishment model
- I try and save both in a transaction block. The client_user.save!
call raises an exception causing the block to roll back. - Yet establishment has an ID set to 10 and is marked as not being a
new record. - As expected client_user has no ID
- Calling reload on establishment throws an error because the row
does not exist - 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?