diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-04-20 19:33:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-20 19:33:47 -0400 |
commit | 7fcd25f44ce059bf9d68b9fd125c44c46b08c074 (patch) | |
tree | f956e9b0d0a96a2d289977ee690a843b15109463 /activerecord/test | |
parent | 32431b37704c0aaec06ae1a23e0d6091d6542fd2 (diff) | |
parent | c0038f7c362fa0c92fc9e1ea3bdb2706f42386c6 (diff) | |
download | rails-7fcd25f44ce059bf9d68b9fd125c44c46b08c074.tar.gz rails-7fcd25f44ce059bf9d68b9fd125c44c46b08c074.tar.bz2 rails-7fcd25f44ce059bf9d68b9fd125c44c46b08c074.zip |
Merge pull request #28640 from kamipo/prevent_double_firing_before_save_callback
Prevent double firing the before save callback of new object when the parent association saved in the callback
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index e2f044c139..25133fc580 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -2447,15 +2447,29 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [first_bulb, second_bulb], car.bulbs end - test "double insertion of new object to association when same association used in the after create callback of a new object" do + test "prevent double insertion of new object when the parent association loaded in the after save callback" do reset_callbacks(:save, Bulb) do Bulb.after_save { |record| record.car.bulbs.load } + car = Car.create! car.bulbs << Bulb.new + assert_equal 1, car.bulbs.size end end + test "prevent double firing the before save callback of new object when the parent association saved in the callback" do + reset_callbacks(:save, Bulb) do + count = 0 + Bulb.before_save { |record| record.car.save && count += 1 } + + car = Car.create! + car.bulbs.create! + + assert_equal 1, count + end + end + class AuthorWithErrorDestroyingAssociation < ActiveRecord::Base self.table_name = "authors" has_many :posts_with_error_destroying, @@ -2496,7 +2510,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_loading_association_in_validate_callback_doesnt_affect_persistence reset_callbacks(:validation, Bulb) do - Bulb.after_validation { |m| m.car.bulbs.load } + Bulb.after_validation { |record| record.car.bulbs.load } car = Car.create!(name: "Car") bulb = car.bulbs.create! |