This project is archived and is in readonly mode.
Model objects are falsely marked as not-new after transaction rollback
Reported by pdietrich | May 26th, 2008 @ 05:26 PM
Consider the following code from a controller action:
begin
@auction.transaction do
@auction.save!
@lot.auction = @auction
@lot.save!
flash[:notice] = 'Auction was successfully created.'
redirect_to(@auction)
end
rescue
render :action => "new"
end
If no validations fail on @auction ActiveRecord considers it saved even if the transaction cannot be finished successfully (e.g. when @lot.save raises an exception). Hence @auction.new_record? will return false although the record is not saved in the DB.
This leads a problem in the view:
form_for(@auction)
will generate a put (to controller action 'update') instead of a post (to controller action 'create').
I think the right way to fix this is to remember which model objects have been saved in a transaction and to revert their state after rollback. Any suggestions how this could be done the best?
Comments and changes to this ticket
-
Xavier Noria May 26th, 2008 @ 06:17 PM
Also timestamps are not rolled back, so the new record may have an initialized created_at. (Related to #93.)
-
Carl Porth May 28th, 2008 @ 06:05 AM
I'm attaching an update to TransactionTest#test_callback_rollback_in_create to expose this bug. Perhaps it will be of use to someone who knows the transaction code well.
-
josh August 29th, 2008 @ 04:41 PM
- State changed from new to stale
- Tag set to 2.0-stable, activerecord, activeresource, tests, transaction
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>