Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6445 from chancancode/3-2-stable_restore_frozen_s…

…tate_on_rollback

Restore the frozen state on rollback. (Backports #6420)
  • Loading branch information...
commit 9ee85281bca899fc521200a087149d7c8c3940d9 2 parents 56b86a3 + a543002
@rafaelfranca rafaelfranca authored
View
4 activerecord/lib/active_record/transactions.rb
@@ -327,7 +327,8 @@ def restore_transaction_record_state(force = false) #:nodoc:
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
if @_start_transaction_state[:level] < 1
restore_state = remove_instance_variable(:@_start_transaction_state)
- @attributes = @attributes.dup if @attributes.frozen?
+ was_frozen = @attributes.frozen?
+ @attributes = @attributes.dup if was_frozen
@new_record = restore_state[:new_record]
@destroyed = restore_state[:destroyed]
if restore_state.has_key?(:id)
@@ -336,6 +337,7 @@ def restore_transaction_record_state(force = false) #:nodoc:
@attributes.delete(self.class.primary_key)
@attributes_cache.delete(self.class.primary_key)
end
+ @attributes.freeze if was_frozen
end
end
end
View
10 activerecord/test/cases/transactions_test.rb
@@ -362,6 +362,16 @@ def test_rollback_when_commit_raises
end
end
+ def test_rollback_when_saving_a_frozen_record
+ topic = Topic.new(:title => 'test')
+ topic.freeze
+ e = assert_raise(RuntimeError) { topic.save }
+ assert_equal "can't modify frozen Hash", e.message
+ assert !topic.persisted?, 'not persisted'
+ assert_nil topic.id
+ assert topic.frozen?, 'not frozen'
+ end
+
def test_restore_active_record_state_for_all_records_in_a_transaction
topic_1 = Topic.new(:title => 'test_1')
topic_2 = Topic.new(:title => 'test_2')

2 comments on commit 9ee8528

@frodsan

@rafaelfranca

In Ruby 1.9.2 -> the exception message is: "can't modify frozen hash"
In Ruby 1.8.7 -> It raise TypeError instead of RuntimeError

@rafaelfranca

@frodsan thanks. I talked with the author and he will submit a pull request

Please sign in to comment.
Something went wrong with that request. Please try again.