From ae028984d91a346067eac3d7f43d9ff3217bed11 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 4 Jun 2018 02:56:52 +0900 Subject: Fix `save` in `after_create_commit` won't invoke extra `after_create_commit` Since a record is already persisted in `after_create_commit`, so `save` should invoke only `after_update_commit`. This bug is caused by depending on `@_start_transaction_state` for rollback to consider whether it was `new_record` before being committed. If after commit callbacks caused another commit, the state before last commit is no longer `new_record`. Fixes #32831. Closes #18367. Closes #31106. --- activerecord/test/cases/transaction_callbacks_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord/test/cases/transaction_callbacks_test.rb') diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index 7c87030801..c0be45eee7 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -147,6 +147,15 @@ class TransactionCallbacksTest < ActiveRecord::TestCase assert_equal [:commit_on_destroy], new_record.history end + def test_save_in_after_create_commit_wont_invoke_extra_after_create_commit + new_record = TopicWithCallbacks.new(title: "New topic", written_on: Date.today) + add_transaction_execution_blocks new_record + new_record.after_commit_block(:create) { |r| r.save! } + + new_record.save! + assert_equal [:commit_on_create, :commit_on_update], new_record.history + end + def test_only_call_after_commit_on_create_and_doesnt_leaky r = ReplyWithCallbacks.new(content: "foo") r.save_on_after_create = true -- cgit v1.2.3