aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLawrence Pit <lawrence.pit@gmail.com>2010-01-07 19:53:15 +0100
committerEloy Duran <eloy.de.enige@gmail.com>2010-01-08 21:45:02 +0100
commit5193fe9dd730f9bbb72db055f37625fe9558b6ca (patch)
tree2f9391a569f5b14b75bcd7e244963c5248b4bf11
parent1080351437dc43c3ecaa0d494f5ca215f03b1883 (diff)
downloadrails-5193fe9dd730f9bbb72db055f37625fe9558b6ca.tar.gz
rails-5193fe9dd730f9bbb72db055f37625fe9558b6ca.tar.bz2
rails-5193fe9dd730f9bbb72db055f37625fe9558b6ca.zip
Exclude unchanged records from the collection being considered for autosave. [#2578 state:resolved]
Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
-rw-r--r--activerecord/lib/active_record/autosave_association.rb6
-rw-r--r--activerecord/test/cases/autosave_association_test.rb13
2 files changed, 7 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index f01d0903cd..741fb2ef40 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -224,10 +224,10 @@ module ActiveRecord
def associated_records_to_validate_or_save(association, new_record, autosave)
if new_record
association
- elsif association.loaded?
- autosave ? association : association.find_all { |record| record.new_record? }
+ elsif autosave
+ association.target.find_all { |record| record.new_record? || record.changed? || record.marked_for_destruction? }
else
- autosave ? association.target : association.target.find_all { |record| record.new_record? }
+ association.target.find_all { |record| record.new_record? }
end
end
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
index 97d75b651b..fd2b0d2d9f 100644
--- a/activerecord/test/cases/autosave_association_test.rb
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -701,23 +701,18 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
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}") }
- 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
- class << before.first
- def save(*args)
- super
- destroy
- end
- end
+ # Stub the destroy method of the the second child to raise an exception
class << before.last
- def save(*args)
+ def destroy(*args)
super
raise 'Oh noes!'
end
end
assert_raise(RuntimeError) { assert !@pirate.save }
+ assert before.first.frozen? # the first child was indeed destroyed
assert_equal before, @pirate.reload.send(association_name)
end