diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-12-29 03:11:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-29 03:11:57 -0500 |
commit | 7ca4b2a76d69d86e1afa92cb0f201f0168815636 (patch) | |
tree | b860fb228b32ec0993aaad15bb517babab3c6b8b | |
parent | 3c90818129e9e295f35c928317c55c111a43d8a9 (diff) | |
parent | 9abf606a79729833fd90f624ed580ad85f3ebadc (diff) | |
download | rails-7ca4b2a76d69d86e1afa92cb0f201f0168815636.tar.gz rails-7ca4b2a76d69d86e1afa92cb0f201f0168815636.tar.bz2 rails-7ca4b2a76d69d86e1afa92cb0f201f0168815636.zip |
Merge pull request #27490 from kamipo/should_not_update_children_when_parent_creation_with_no_reason
Should not update children when the parent creation with no reason
4 files changed, 20 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 0c0aefe3b9..c4a7fe4432 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -38,10 +38,12 @@ module ActiveRecord def insert_record(record, validate = true, raise = false) ensure_not_nested - if raise - record.save!(validate: validate) - else - return unless record.save(validate: validate) + if record.new_record? || record.has_changes_to_save? + if raise + record.save!(validate: validate) + else + return unless record.save(validate: validate) + end end save_through_record(record) diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index eb80ae4f7c..6d31b7a091 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -1391,6 +1391,14 @@ module AutosaveAssociationOnACollectionAssociationTests assert_equal "Squawky", parrot.reload.name end + def test_should_not_update_children_when_parent_creation_with_no_reason + parrot = Parrot.create!(name: "Polly") + assert_equal 0, parrot.updated_count + + Pirate.create!(parrot_ids: [parrot.id], catchphrase: "Arrrr") + assert_equal 0, parrot.reload.updated_count + end + def test_should_automatically_validate_the_associated_models @pirate.send(@association_name).each { |child| child.name = "" } diff --git a/activerecord/test/models/parrot.rb b/activerecord/test/models/parrot.rb index 5b693664d4..1e5f9285a8 100644 --- a/activerecord/test/models/parrot.rb +++ b/activerecord/test/models/parrot.rb @@ -13,6 +13,11 @@ class Parrot < ActiveRecord::Base def cancel_save_callback_method throw(:abort) end + + before_update :increment_updated_count + def increment_updated_count + self.updated_count += 1 + end end class LiveParrot < Parrot diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 9d76d0537e..ba6f5de894 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -574,6 +574,7 @@ ActiveRecord::Schema.define do t.column :color, :string t.column :parrot_sti_class, :string t.column :killer_id, :integer + t.column :updated_count, :integer, default: 0 if subsecond_precision_supported? t.column :created_at, :datetime, precision: 0 t.column :created_on, :datetime, precision: 0 |