Skip to content
This repository
Browse code

Exclude unchanged records from the collection being considered for au…

…tosave. [#2578 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information...
commit 5193fe9dd730f9bbb72db055f37625fe9558b6ca 1 parent 1080351
Lawrence Pit authored January 07, 2010 alloy committed January 08, 2010
6  activerecord/lib/active_record/autosave_association.rb
@@ -224,10 +224,10 @@ def marked_for_destruction?
224 224
     def associated_records_to_validate_or_save(association, new_record, autosave)
225 225
       if new_record
226 226
         association
227  
-      elsif association.loaded?
228  
-        autosave ? association : association.find_all { |record| record.new_record? }
  227
+      elsif autosave
  228
+        association.target.find_all { |record| record.new_record? || record.changed? || record.marked_for_destruction? }
229 229
       else
230  
-        autosave ? association.target : association.target.find_all { |record| record.new_record? }
  230
+        association.target.find_all { |record| record.new_record? }
231 231
       end
232 232
     end
233 233
 
13  activerecord/test/cases/autosave_association_test.rb
@@ -701,23 +701,18 @@ def save(*args)
701 701
 
702 702
     define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
703 703
       2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
704  
-      before = @pirate.send(association_name).map { |c| c }
  704
+      before = @pirate.send(association_name).map { |c| c.mark_for_destruction ; c }
705 705
 
706  
-      # Stub the save method of the first child to destroy and the second to raise an exception
707  
-      class << before.first
708  
-        def save(*args)
709  
-          super
710  
-          destroy
711  
-        end
712  
-      end
  706
+      # Stub the destroy method of the the second child to raise an exception
713 707
       class << before.last
714  
-        def save(*args)
  708
+        def destroy(*args)
715 709
           super
716 710
           raise 'Oh noes!'
717 711
         end
718 712
       end
719 713
 
720 714
       assert_raise(RuntimeError) { assert !@pirate.save }
  715
+      assert before.first.frozen? # the first child was indeed destroyed
721 716
       assert_equal before, @pirate.reload.send(association_name)
722 717
     end
723 718
 

0 notes on commit 5193fe9

Please sign in to comment.
Something went wrong with that request. Please try again.