mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Exclude unchanged records from the collection being considered for autosave. [#2578 state:resolved]
Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
This commit is contained in:
parent
1080351437
commit
5193fe9dd7
2 changed files with 7 additions and 12 deletions
|
@ -224,10 +224,10 @@ module ActiveRecord
|
||||||
def associated_records_to_validate_or_save(association, new_record, autosave)
|
def associated_records_to_validate_or_save(association, new_record, autosave)
|
||||||
if new_record
|
if new_record
|
||||||
association
|
association
|
||||||
elsif association.loaded?
|
elsif autosave
|
||||||
autosave ? association : association.find_all { |record| record.new_record? }
|
association.target.find_all { |record| record.new_record? || record.changed? || record.marked_for_destruction? }
|
||||||
else
|
else
|
||||||
autosave ? association.target : association.target.find_all { |record| record.new_record? }
|
association.target.find_all { |record| record.new_record? }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -701,23 +701,18 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
|
||||||
|
|
||||||
define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
|
define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
|
||||||
2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
|
2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
|
||||||
before = @pirate.send(association_name).map { |c| c }
|
before = @pirate.send(association_name).map { |c| c.mark_for_destruction ; c }
|
||||||
|
|
||||||
# Stub the save method of the first child to destroy and the second to raise an exception
|
# Stub the destroy method of the the second child to raise an exception
|
||||||
class << before.first
|
|
||||||
def save(*args)
|
|
||||||
super
|
|
||||||
destroy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
class << before.last
|
class << before.last
|
||||||
def save(*args)
|
def destroy(*args)
|
||||||
super
|
super
|
||||||
raise 'Oh noes!'
|
raise 'Oh noes!'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_raise(RuntimeError) { assert !@pirate.save }
|
assert_raise(RuntimeError) { assert !@pirate.save }
|
||||||
|
assert before.first.frozen? # the first child was indeed destroyed
|
||||||
assert_equal before, @pirate.reload.send(association_name)
|
assert_equal before, @pirate.reload.send(association_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue