Skip to content

Commit

Permalink
Merge pull request rails#20947
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrif committed Jul 20, 2015
2 parents c0ef95a + 12b0b26 commit c0f79be
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def assert_filtered_out(params, key)
assert_equal nil, @params.fetch(:foo) { nil }
end

test 'KeyError in fetch block should not be coverd up' do
test 'KeyError in fetch block should not be covered up' do
params = ActionController::Parameters.new
e = assert_raises(KeyError) do
params.fetch(:missing_key) { {}.fetch(:also_missing) }
Expand Down
14 changes: 14 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
* Fix state being carried over from previous transaction.

Considering the following example where `name` is a required attribute.
Before we had `new_record?` returning `true` for a persisted record:

author = Author.create! name: 'foo'
author.name = nil
author.save # => false
author.new_record? # => true

Fixes #20824.

*Roque Pinel*

* Correctly ignore `mark_for_destruction` when `autosave` isn't set to `true`
when validating associations.

Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ def with_transaction_returning_status
raise ActiveRecord::Rollback unless status
end
status
ensure
if @transaction_state && @transaction_state.committed?
clear_transaction_record_state
end
end

protected
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ def topic.after_save_for_transaction
assert topic.new_record?, "#{topic.inspect} should be new record"
end

def test_transaction_state_is_cleared_when_record_is_persisted
author = Author.create! name: 'foo'
author.name = nil
assert_not author.save
assert_not author.new_record?
end

def test_update_should_rollback_on_failure
author = Author.find(1)
posts_count = author.posts.size
Expand Down

0 comments on commit c0f79be

Please sign in to comment.