aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/integration.rb17
-rw-r--r--activerecord/test/cases/cache_key_test.rb8
2 files changed, 19 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb
index 43f6afbb42..db7200bceb 100644
--- a/activerecord/lib/active_record/integration.rb
+++ b/activerecord/lib/active_record/integration.rb
@@ -97,13 +97,18 @@ module ActiveRecord
# +false+ (which it is by default until Rails 6.0).
def cache_version
return unless cache_versioning
- return unless has_attribute?("updated_at")
- timestamp = updated_at_before_type_cast
- if can_use_fast_cache_version?(timestamp)
- raw_timestamp_to_cache_version(timestamp)
- elsif timestamp = updated_at
- timestamp.utc.to_s(cache_timestamp_format)
+ if has_attribute?("updated_at")
+ timestamp = updated_at_before_type_cast
+ if can_use_fast_cache_version?(timestamp)
+ raw_timestamp_to_cache_version(timestamp)
+ elsif timestamp = updated_at
+ timestamp.utc.to_s(cache_timestamp_format)
+ end
+ else
+ if self.class.has_attribute?("updated_at")
+ raise ActiveModel::MissingAttributeError, "missing attribute: updated_at"
+ end
end
end
diff --git a/activerecord/test/cases/cache_key_test.rb b/activerecord/test/cases/cache_key_test.rb
index 535511d1cb..3a06b1c795 100644
--- a/activerecord/test/cases/cache_key_test.rb
+++ b/activerecord/test/cases/cache_key_test.rb
@@ -119,5 +119,13 @@ module ActiveRecord
record_from_db.cache_version
end
end
+
+ test "updated_at on class but not on instance raises an error" do
+ record = CacheMeWithVersion.create
+ record_from_db = CacheMeWithVersion.where(id: record.id).select(:id).first
+ assert_raises(ActiveModel::MissingAttributeError) do
+ record_from_db.cache_version
+ end
+ end
end
end