diff options
author | Steve Klabnik <steve@steveklabnik.com> | 2013-02-13 12:30:01 -0800 |
---|---|---|
committer | Steve Klabnik <steve@steveklabnik.com> | 2013-02-13 12:30:01 -0800 |
commit | f36b80b3e8a1aae9e45af7c82be3917bc6277aa1 (patch) | |
tree | 2271c8fc2593bf4afd7158165ac7d5c9a9a66a0a /activerecord/test | |
parent | 5d58948fe72e3b0422790b8adc0fab7bbf9e6573 (diff) | |
parent | caabed6c76eea0db99949f34c234ef1b2657392a (diff) | |
download | rails-f36b80b3e8a1aae9e45af7c82be3917bc6277aa1.tar.gz rails-f36b80b3e8a1aae9e45af7c82be3917bc6277aa1.tar.bz2 rails-f36b80b3e8a1aae9e45af7c82be3917bc6277aa1.zip |
Merge pull request #9115 from bensie/issue_5802
Don't call after_commit when creating through an association and save fails, fixes #5802
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/transaction_callbacks_test.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index 869892e33f..fd5651b4e0 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -5,9 +5,29 @@ class TransactionCallbacksTest < ActiveRecord::TestCase self.use_transactional_fixtures = false fixtures :topics + class ReplyWithCallbacks < ActiveRecord::Base + self.table_name = :topics + + belongs_to :topic, foreign_key: "parent_id" + + validates_presence_of :content + + after_commit :do_after_commit, on: :create + + def history + @history ||= [] + end + + def do_after_commit + history << :commit_on_create + end + end + class TopicWithCallbacks < ActiveRecord::Base self.table_name = :topics + has_many :replies, class_name: "ReplyWithCallbacks", foreign_key: "parent_id" + after_commit{|record| record.send(:do_after_commit, nil)} after_commit(:on => :create){|record| record.send(:do_after_commit, :create)} after_commit(:on => :update){|record| record.send(:do_after_commit, :update)} @@ -93,6 +113,13 @@ class TransactionCallbacksTest < ActiveRecord::TestCase assert_equal [:commit_on_create], @new_record.history end + def test_only_call_after_commit_on_create_after_transaction_commits_for_new_record_if_create_succeeds_creating_through_association + topic = TopicWithCallbacks.create!(:title => "New topic", :written_on => Date.today) + reply = topic.replies.create + + assert_equal [], reply.history + end + def test_call_after_rollback_after_transaction_rollsback @first.after_commit_block{|r| r.history << :after_commit} @first.after_rollback_block{|r| r.history << :after_rollback} |