Skip to content

Commit

Permalink
Merge pull request #11451 from jetthoughts/11450_do_not_resave_destro…
Browse files Browse the repository at this point in the history
…yed_association

Do not re-save destroyed association on saving parent object
Conflicts:
	activerecord/lib/active_record/autosave_association.rb

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
senny authored and rafaelfranca committed Jul 15, 2013
1 parent 714cb5a commit 1c2545a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -339,6 +339,8 @@ def save_collection_association(reflection)
end end


records.each do |record| records.each do |record|
next if record.destroyed?

saved = true saved = true


if autosave != false && (@new_record_before_save || record.new_record?) if autosave != false && (@new_record_before_save || record.new_record?)
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -734,6 +734,13 @@ def test_should_destroy_has_many_as_part_of_the_save_transaction_if_they_were_ma
ids.each { |id| assert_nil klass.find_by_id(id) } ids.each { |id| assert_nil klass.find_by_id(id) }
end end


def test_should_not_resave_destroyed_association
@pirate.birds.create!(name: :parrot)
@pirate.birds.first.destroy
@pirate.save!
assert @pirate.reload.birds.empty?
end

def test_should_skip_validation_on_has_many_if_marked_for_destruction def test_should_skip_validation_on_has_many_if_marked_for_destruction
2.times { |i| @pirate.birds.create!(:name => "birds_#{i}") } 2.times { |i| @pirate.birds.create!(:name => "birds_#{i}") }


Expand Down

0 comments on commit 1c2545a

Please sign in to comment.