Fix `ActiveRecord::Relation#touch_all` with custom attribute aliased as attribute for update

This commit is contained in:
fatkodima 2024-05-11 14:14:44 +03:00
parent be0cb4e8f9
commit 5dcc35b6e1
2 changed files with 16 additions and 1 deletions

View File

@ -54,8 +54,10 @@ module ActiveRecord
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

View File

@ -135,6 +135,19 @@ class UpdateAllTest < ActiveRecord::TestCase
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