From 713fc39d93601043e027d5cd9ebcfa08064f9417 Mon Sep 17 00:00:00 2001 From: Brock Trappitt Date: Mon, 5 May 2014 20:24:04 +0800 Subject: [PATCH] Prevented belongs_to: touch propagating up if there are no changes being saved --- activerecord/CHANGELOG.md | 8 ++++++++ .../lib/active_record/associations/builder/belongs_to.rb | 2 +- .../cases/associations/belongs_to_associations_test.rb | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 55a3cbfd60..447a000e15 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,11 @@ +* Change belongs_to touch to be consistent with timestamp updates + + If a model is set up with a belongs_to: touch relatinoship the parent + record will only be touched if the record was modified. This makes it + consistent with timestamp updating on the record itself. + + *Brock Trappitt* + * Fixed the inferred table name of a HABTM auxiliar table inside a schema. Fixes #14824 diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 47cc1f4b34..3998aca23e 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -103,7 +103,7 @@ module ActiveRecord::Associations::Builder BelongsTo.touch_record(record, foreign_key, n, touch) } - model.after_save callback + model.after_save callback, if: :changed? model.after_touch callback model.after_destroy callback end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 3b484a0d64..dc7314b450 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -369,6 +369,13 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_queries(2) { line_item.update amount: 10 } end + def test_belongs_to_with_touch_option_on_empty_update + line_item = LineItem.create! + Invoice.create!(line_items: [line_item]) + + assert_queries(0) { line_item.save } + end + def test_belongs_to_with_touch_option_on_destroy line_item = LineItem.create! Invoice.create!(line_items: [line_item])