Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Model objects are falsely marked as not-new after transaction rollback

#256

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?

Reported by pdietrich · May 26th, 2008 @ 05:26 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Xavier Noria
    Xavier Noria

    Also timestamps are not rolled back, so the new record may have an initialized created_at. (Related to #93 Restore timestamps in model rollback.)

    May 26th, 2008 @ 06:17 PM

  2. Carl Porth
    Carl Porth

    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.

    May 28th, 2008 @ 06:05 AM

  3. josh
    josh
    • State changed from new to stale
    • Tag set to 2.0-stable, activerecord, activeresource, tests, transaction

    August 29th, 2008 @ 04:41 PM