aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/collection_cache_key.rb1
-rw-r--r--activerecord/test/cases/collection_cache_key_test.rb6
2 files changed, 7 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/collection_cache_key.rb b/activerecord/lib/active_record/collection_cache_key.rb
index b20df1c232..5dcc98424a 100644
--- a/activerecord/lib/active_record/collection_cache_key.rb
+++ b/activerecord/lib/active_record/collection_cache_key.rb
@@ -15,6 +15,7 @@ module ActiveRecord
column = "#{connection.quote_table_name(collection.table_name)}.#{connection.quote_column_name(timestamp_column)}"
query = collection
+ .unscope(:select)
.select("COUNT(*) AS size", "MAX(#{column}) AS timestamp")
.unscope(:order)
result = connection.select_one(query)
diff --git a/activerecord/test/cases/collection_cache_key_test.rb b/activerecord/test/cases/collection_cache_key_test.rb
index 6b34979e4a..a2874438c1 100644
--- a/activerecord/test/cases/collection_cache_key_test.rb
+++ b/activerecord/test/cases/collection_cache_key_test.rb
@@ -79,5 +79,11 @@ module ActiveRecord
developers = Developer.offset(20)
assert_match(/\Adevelopers\/query-(\h+)-0\Z/, developers.cache_key)
end
+
+ test "cache_key with a relation having selected columns" do
+ developers = Developer.select(:salary)
+
+ assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\Z/, developers.cache_key)
+ end
end
end