aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorschneems <richard.schneeman+foo@gmail.com>2018-10-15 15:31:12 -0500
committerschneems <richard.schneeman+foo@gmail.com>2018-10-17 11:05:05 -0500
commit2f99da00c7b311af0bc5969985eee97937790e4f (patch)
tree971997f10298c1a6ea27d53dde9f1688d5a150f4 /activerecord/lib/active_record
parent04454839a1a07cacac58cdf756a6b8e3adde0ef5 (diff)
downloadrails-2f99da00c7b311af0bc5969985eee97937790e4f.tar.gz
rails-2f99da00c7b311af0bc5969985eee97937790e4f.tar.bz2
rails-2f99da00c7b311af0bc5969985eee97937790e4f.zip
Do not silently fail to generate a cache_version
When an `updated_at` column exists on the model, but is not available on the instance (likely due to a select), we should raise an error rather than silently not generating a cache_version. Without this behavior it's likely that cache entries will not be able to be invalidated and this will happen without notice. This behavior was reported and described by @lsylvester in https://github.com/rails/rails/pull/34197#issuecomment-429668759.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/integration.rb17
1 files changed, 11 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