1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix AR::Relation#cache_key to remove select scope added by user

- We don't need the select scope added by user as we only want to max
  timestamp and size of the collection. So we already know which columns
  to select.
- Additionally having user defined columns in select scope blows the cache_key
  method with PostGreSQL because it needs all `selected` columns in the group_by
  clause or aggregate function.
- Fixes #23038.
This commit is contained in:
Prathamesh Sonpatki 2016-01-14 10:21:28 +05:30
parent 62aa850fee
commit 29cf0dd9f4
2 changed files with 7 additions and 0 deletions

View file

@ -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)

View file

@ -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