From 3720875602141dda285f5a62a8dd33e4c9ba62ba Mon Sep 17 00:00:00 2001 From: Bryan Stearns Date: Wed, 30 Sep 2009 18:21:23 -0700 Subject: [PATCH 1/2] Exclude unchanged records from the collection being considered (per Lawrence Pit's suggestion) --- .../lib/active_record/autosave_association.rb | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index d7fdb0b..e5ca118 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -221,12 +221,12 @@ module ActiveRecord def associated_records_to_validate_or_save(association, new_record, autosave) if new_record association - elsif association.loaded? - autosave ? association : association.select { |record| record.new_record? } + elsif autosave + association.select { |record| record.new_record? || record.changed? } else - autosave ? association.target : association.target.select { |record| record.new_record? } + association.select { |record| record.new_record? } end - end + end # Validate the association if :validate or :autosave is # turned on for the association specified by +reflection+. @@ -362,4 +362,4 @@ module ActiveRecord end end end -end \ No newline at end of file +end -- 1.6.4.4 From f1aa2fe8199f0960fb43ab05db0418cc15121598 Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Thu, 1 Oct 2009 13:34:48 +1000 Subject: [PATCH 2/2] Autosave changes only. --- .../lib/active_record/autosave_association.rb | 6 +++--- .../test/cases/autosave_association_test.rb | 13 ++++--------- 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 e5ca118..49b5c35 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -222,11 +222,11 @@ module ActiveRecord if new_record association elsif autosave - association.select { |record| record.new_record? || record.changed? } + association.target.select { |record| record.new_record? || record.changed? || record.marked_for_destruction? } else - association.select { |record| record.new_record? } + association.target.select { |record| record.new_record? } end - end + end # Validate the association if :validate or :autosave is # turned on for the association specified by +reflection+. diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index d91f90a..1377a85 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -663,23 +663,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 -- 1.6.4.4