This project is archived and is in readonly mode.

#256 ✓stale
pdietrich

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

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>

Attachments

Pages