Skip to content

Commit 5dcc35b

Browse files
committed
Fix ActiveRecord::Relation#touch_all with custom attribute aliased as attribute for update
1 parent be0cb4e commit 5dcc35b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

activerecord/lib/active_record/timestamp.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ def initialize_dup(other) # :nodoc:
5454

5555
module ClassMethods # :nodoc:
5656
def touch_attributes_with_time(*names, time: nil)
57+
names = names.map(&:to_s)
58+
names = names.map { |name| attribute_aliases[name] || name }
5759
attribute_names = timestamp_attributes_for_update_in_model
58-
attribute_names |= names.map(&:to_s)
60+
attribute_names |= names
5961
attribute_names.index_with(time || current_time_from_proper_timezone)
6062
end
6163

activerecord/test/cases/relation/update_all_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ def test_touch_all_with_custom_timestamp
135135
assert_not_equal previously_updated_at, developer.updated_at
136136
end
137137

138+
def test_touch_all_with_aliased_for_update_timestamp
139+
assert Developer.attribute_aliases.key?("updated_at")
140+
141+
developer = developers(:david)
142+
previously_created_at = developer.created_at
143+
previously_updated_at = developer.updated_at
144+
Developer.where(name: "David").touch_all(:updated_at)
145+
developer.reload
146+
147+
assert_equal previously_created_at, developer.created_at
148+
assert_not_equal previously_updated_at, developer.updated_at
149+
end
150+
138151
def test_touch_all_with_given_time
139152
developer = developers(:david)
140153
previously_created_at = developer.created_at

0 commit comments

Comments
 (0)