From 66c8a46cfc193592439c9c60e8c5146b5655df3b Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 14 May 2024 03:55:58 +0900 Subject: [PATCH] Merge pull request #51787 from fatkodima/fix-touch_all-with-aliased-attribute-for-update Fix `ActiveRecord::Relation#touch_all` with custom attribute aliased as attribute for update --- activerecord/lib/active_record/timestamp.rb | 4 +++- activerecord/test/cases/relation/update_all_test.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index 6f60e502f0991..dbea3db136fea 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -54,8 +54,10 @@ def initialize_dup(other) # :nodoc: module ClassMethods # :nodoc: def touch_attributes_with_time(*names, time: nil) + names = names.map(&:to_s) + names = names.map { |name| attribute_aliases[name] || name } attribute_names = timestamp_attributes_for_update_in_model - attribute_names |= names.map(&:to_s) + attribute_names |= names attribute_names.index_with(time || current_time_from_proper_timezone) end diff --git a/activerecord/test/cases/relation/update_all_test.rb b/activerecord/test/cases/relation/update_all_test.rb index d41108b1c4058..1103215db9018 100644 --- a/activerecord/test/cases/relation/update_all_test.rb +++ b/activerecord/test/cases/relation/update_all_test.rb @@ -135,6 +135,19 @@ def test_touch_all_with_custom_timestamp assert_not_equal previously_updated_at, developer.updated_at end + def test_touch_all_with_aliased_for_update_timestamp + assert Developer.attribute_aliases.key?("updated_at") + + developer = developers(:david) + previously_created_at = developer.created_at + previously_updated_at = developer.updated_at + Developer.where(name: "David").touch_all(:updated_at) + developer.reload + + assert_equal previously_created_at, developer.created_at + assert_not_equal previously_updated_at, developer.updated_at + end + def test_touch_all_with_given_time developer = developers(:david) previously_created_at = developer.created_at