Fix `cache_key` with a relation having custom select and order

We can't replace existing select list as long as referenced by ORDER BY.
This commit is contained in:
Ryuta Kamizono 2017-12-29 22:10:24 +09:00
parent dab7d401e8
commit c10c21f2e1
2 changed files with 7 additions and 2 deletions

View File

@ -20,8 +20,7 @@ module ActiveRecord
select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp"
if collection.has_limit_or_offset?
query = collection.spawn
query.select_values = [column]
query = collection.select(column)
subquery_alias = "subquery_for_cache_key"
subquery_column = "#{subquery_alias}.#{timestamp_column}"
subquery = query.arel.as(subquery_alias)

View File

@ -141,5 +141,11 @@ module ActiveRecord
assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, developers.cache_key)
end
test "cache_key with a relation having custom select and order" do
developers = Developer.select("name AS dev_name").order("dev_name DESC").limit(5)
assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, developers.cache_key)
end
end
end