From a08553d93807377281d9be910f789428863fabe4 Mon Sep 17 00:00:00 2001 From: Subba Rao Pasupuleti Date: Wed, 14 Jul 2010 05:01:54 -0400 Subject: [PATCH] save on parent should not cascade to child unless child changed [#3353 state:resolved] --- .../lib/active_record/autosave_association.rb | 6 ++++-- .../test/cases/autosave_association_test.rb | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 7517896..4509d42 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -372,7 +372,9 @@ module ActiveRecord if autosave && association.marked_for_destruction? association.destroy elsif autosave != false - saved = association.save(:validate => !autosave) if association.new_record? || autosave + if association.new_record? || ( autosave && association.changed? ) + saved = association.save(:validate => !autosave) + end if association.updated? association_id = association.send(reflection.options[:primary_key] || :id) @@ -388,4 +390,4 @@ module ActiveRecord end end end -end \ No newline at end of file +end diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 3b89c12..00d4aa5 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -632,6 +632,9 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase end def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_parent + #association save method only trigged when association is changed + @ship.pirate.catchphrase = "new catch phrase" + # Stub the save method of the @ship.pirate instance to destroy and then raise an exception class << @ship.pirate def save(*args) @@ -880,6 +883,22 @@ class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase @pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?") end + def test_should_not_call_belongs_to_after_save_callbacks_if_no_changes + @ship.attributes = { :name => "Titanic", :pirate_attributes => {:id => @pirate.id} } + #here there are no changes to pirate so if save on ship causes save on pirate + #this callback will fail pirate save.(pirate save shouldn't happen) + @ship.pirate.cancel_save_from_callback = true + @ship.save + assert_equal 'Titanic', @ship.reload.name + end + + def test_should_call_belongs_to_save_if_belongs_to_has_changes + @ship.attributes = { :name => "Titanic", :pirate_attributes => { :catchphrase => 'Jack', :id => @pirate.id} } + @ship.save + assert_equal 'Titanic', @ship.reload.name + assert_equal 'Jack', @pirate.reload.catchphrase + end + def test_should_still_work_without_an_associated_model @pirate.destroy @ship.reload.name = "The Vile Insanity" -- 1.7.0.4