Open keating opened 12 years ago
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 transaction file,
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: 44d1804b0a86de02865c48c552bbc57da3dc7836 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 restore to when the transaction roll back.
Num: #44d1804b0a86de02865c48c552bbc57da3dc7836
aaaaaaaaaa