diff options
| author | Chiel Wester <chiel.wester@holder.nl> | 2010-12-13 15:06:23 +0100 | 
|---|---|---|
| committer | José Valim <jose.valim@gmail.com> | 2010-12-13 17:04:57 +0100 | 
| commit | 658bb4fa25db0b3f61bfb64028274f2365cad506 (patch) | |
| tree | d52f85f6b691a44640172c3a870e0d39fb4a8c33 | |
| parent | 32a2bf8d4e170ce68f895c007da852211892ba5e (diff) | |
| download | rails-658bb4fa25db0b3f61bfb64028274f2365cad506.tar.gz rails-658bb4fa25db0b3f61bfb64028274f2365cad506.tar.bz2 rails-658bb4fa25db0b3f61bfb64028274f2365cad506.zip | |
Only call save on belongs_to associations if the record has changed or any nested associations have changed (resolves #3353)
Signed-off-by: José Valim <jose.valim@gmail.com>
| -rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 2 | ||||
| -rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 11 | 
2 files changed, 12 insertions, 1 deletions
| diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index c3dda29d03..4a18719551 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -363,7 +363,7 @@ module ActiveRecord          if autosave && association.marked_for_destruction?            association.destroy          elsif autosave != false -          saved = association.save(:validate => !autosave) if association.new_record? || autosave +          saved = association.save(:validate => !autosave) if association.new_record? || (autosave && association.changed_for_autosave?)            if association.updated?              association_id = association.send(reflection.options[:primary_key] || :id) diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index fbf7121468..27aee400f9 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -667,10 +667,21 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase        end      end +    @ship.pirate.catchphrase = "Changed Catchphrase" +      assert_raise(RuntimeError) { assert !@ship.save }      assert_not_nil @ship.reload.pirate    end +  def test_should_save_changed_child_objects_if_parent_is_saved +    @pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?") +    @parrot = @pirate.parrots.create!(:name => 'Posideons Killer') +    @parrot.name = "NewName" +    @ship.save + +    assert_equal 'NewName', @parrot.reload.name +  end +    # has_many & has_and_belongs_to    %w{ parrots birds }.each do |association_name|      define_method("test_should_destroy_#{association_name}_as_part_of_the_save_transaction_if_they_were_marked_for_destroyal") do | 
