diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-03-10 11:41:55 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-03-10 11:41:55 -0300 |
commit | 6e3ab3e15faf782f6a937ccf5574a4fb63e3e353 (patch) | |
tree | 5a4798cd67b536acaa6e29f2234b4a12df7c1b02 /activerecord | |
parent | 39e07b64ce3f4bb55e60ba0266e677f8e4f4893a (diff) | |
parent | f74a5616e89f03e13a025401475ac1101bb159ae (diff) | |
download | rails-6e3ab3e15faf782f6a937ccf5574a4fb63e3e353.tar.gz rails-6e3ab3e15faf782f6a937ccf5574a4fb63e3e353.tar.bz2 rails-6e3ab3e15faf782f6a937ccf5574a4fb63e3e353.zip |
Merge pull request #8313 from alan/only_save_changed_has_one_objects
Save has_one associations only if record has changes
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 15 |
3 files changed, 24 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 3bd85976a0..300c3761f1 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Only save has_one associations if record has changes. + Previously after save related callbacks, such as `#after_commit`, were triggered when the has_one + object did not get saved to the db. + + *Alan Kennedy* + * Allow strings to specify the `#order` value. Example: diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index e9622ca0c1..213b72b933 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -377,15 +377,16 @@ module ActiveRecord def save_has_one_association(reflection) association = association_instance_get(reflection.name) record = association && association.load_target + if record && !record.destroyed? autosave = reflection.options[:autosave] if autosave && record.marked_for_destruction? record.destroy - else + elsif autosave != false key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id - if autosave != false && (autosave || new_record? || record_changed?(reflection, record, key)) + if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key) unless reflection.through_reflection record[reflection.foreign_key] = key end diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index f7584c3a51..8e3825999d 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -683,10 +683,25 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase end end + @ship.pirate.catchphrase = "Changed Catchphrase" + assert_raise(RuntimeError) { assert !@pirate.save } assert_not_nil @pirate.reload.ship end + def test_should_save_changed_has_one_changed_object_if_child_is_saved + @pirate.ship.name = "NewName" + @pirate.ship.expects(:save).once.returns(true) + + assert @pirate.save + end + + def test_should_not_save_changed_has_one_unchanged_object_if_child_is_saved + @pirate.ship.expects(:save).never + + assert @pirate.save + end + # belongs_to def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal assert !@ship.pirate.marked_for_destruction? |