Skip to content

Commit

Permalink
Merge pull request #27490 from kamipo/should_not_update_children_when…
Browse files Browse the repository at this point in the history
…_parent_creation_with_no_reason

Should not update children when the parent creation with no reason
  • Loading branch information
rafaelfranca committed Dec 29, 2016
1 parent 576bfc5 commit 38d04d3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
Expand Up @@ -38,10 +38,12 @@ def concat_records(records)
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)
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -1390,6 +1390,14 @@ def test_should_update_children_when_autosave_is_true_and_parent_is_new_but_chil
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 = '' }

Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/models/parrot.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -568,6 +568,7 @@
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
Expand Down

0 comments on commit 38d04d3

Please sign in to comment.