mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Merge pull request #6935 from frodsan/b1e509ad7a8c8264544f10f4666705cd806b5408
Backport #3329 to 3-2-stable
This commit is contained in:
@@ -332,25 +332,31 @@ module ActiveRecord
|
||||
|
||||
if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
|
||||
begin
|
||||
records.each do |record|
|
||||
next if record.destroyed?
|
||||
records_to_destroy = []
|
||||
|
||||
saved = true
|
||||
records.each do |record|
|
||||
next if record.destroyed?
|
||||
|
||||
if autosave && record.marked_for_destruction?
|
||||
association.proxy.destroy(record)
|
||||
elsif autosave != false && (@new_record_before_save || record.new_record?)
|
||||
if autosave
|
||||
saved = association.insert_record(record, false)
|
||||
else
|
||||
association.insert_record(record) unless reflection.nested?
|
||||
saved = true
|
||||
|
||||
if autosave && record.marked_for_destruction?
|
||||
records_to_destroy << record
|
||||
elsif autosave != false && (@new_record_before_save || record.new_record?)
|
||||
if autosave
|
||||
saved = association.insert_record(record, false)
|
||||
else
|
||||
association.insert_record(record) unless reflection.nested?
|
||||
end
|
||||
elsif autosave
|
||||
saved = record.save(:validate => false)
|
||||
end
|
||||
elsif autosave
|
||||
saved = record.save(:validate => false)
|
||||
|
||||
raise ActiveRecord::Rollback unless saved
|
||||
end
|
||||
|
||||
raise ActiveRecord::Rollback unless saved
|
||||
end
|
||||
records_to_destroy.each do |record|
|
||||
association.proxy.destroy(record)
|
||||
end
|
||||
rescue
|
||||
records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled?
|
||||
raise
|
||||
|
||||
@@ -770,6 +770,16 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
|
||||
assert_equal before, @pirate.reload.birds
|
||||
end
|
||||
|
||||
def test_when_new_record_a_child_marked_for_destruction_should_not_affect_other_records_from_saving
|
||||
@pirate = @ship.build_pirate(:catchphrase => "Arr' now I shall keep me eye on you matey!") # new record
|
||||
|
||||
3.times { |i| @pirate.birds.build(:name => "birds_#{i}") }
|
||||
@pirate.birds[1].mark_for_destruction
|
||||
@pirate.save!
|
||||
|
||||
assert_equal 2, @pirate.birds.reload.length
|
||||
end
|
||||
|
||||
# Add and remove callbacks tests for association collections.
|
||||
%w{ method proc }.each do |callback_type|
|
||||
define_method("test_should_run_add_callback_#{callback_type}s_for_has_many") do
|
||||
|
||||
Reference in New Issue
Block a user