Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

State be restored to when transaction rolls back is not right? #7807

Closed
keating opened this Issue · 7 comments

5 participants

@keating
u = User.new(:name => "jiwen")

begin
  User.transaction do
    u.save
    u.save
    raise "exception"
  end
rescue
end

u.instance_eval { puts @new_record }

I get the output "false" in the above code.
This is a problem. For example, when I create an object in the new page, save the object and then update some attributes with the above code, I will be token to be the edit page. So an error happens, for "cannot find record with the object's id in the database".
I see the code in the file transactions.rb,

def remember_transaction_record_state
    @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
    @_start_transaction_state[:new_record] = @new_record
    @_start_transaction_state[:destroyed] = @destroyed
    @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
end

So, to resolve this problem, just need to change this method to

def remember_transaction_record_state
    @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
    unless @_start_transaction_state.include?(:new_record)
      @_start_transaction_state[:new_record] = @new_record
    end
    unless @_start_transaction_state.include?(:destroyed)
      @_start_transaction_state[:destroyed] = @destroyed
    end
    @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
end

But see this commit, * SHA: 44d1804 which just do the opposite thing for "Fix transaction state not changing when after record gets commited", so this approach is not comfortable to resolve the problem. I think there maybe need a 'state_before_transaction' to record the sate of an object before the transaction and which should be restored to when the transaction rolls back.

@rafaelfranca

Could you check 4.0.0 or master branch? Thanks

@iantropov

I've reproduced this on master branch with this gist https://gist.github.com/iantropov/7394507
So, I will go ahead and will find ways to fix this.

@iantropov iantropov referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@iantropov

I've fixed this problem.
@keating @rafaelfranca please take a look.

@keating

@rafaelfranca Rails 4.0.0 still has this problem

@iantropov iantropov referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@keating keating added the stale label
@rafaelfranca
Owner

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@chancancode
Owner

This seems fixed on 4.1 and master. See #8577 (comment).

@chancancode chancancode closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.