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

Merge pull request #38099 from alipman88/avoid_unecessary_query_if_cache_versioning_enabled

Avoid unnecessary SQL query when calling ActiveRecord::Relation#cache_key
This commit is contained in:
Eileen M. Uchitelle 2020-01-06 08:52:41 -05:00 committed by GitHub
commit be40e8e5af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -317,7 +317,7 @@ module ActiveRecord
query_signature = ActiveSupport::Digest.hexdigest(to_sql)
key = "#{klass.model_name.cache_key}/query-#{query_signature}"
if cache_version(timestamp_column)
if collection_cache_versioning
key
else
"#{key}-#{compute_cache_version(timestamp_column)}"

View file

@ -113,6 +113,13 @@ module ActiveRecord
assert_no_queries { developers.cache_key }
end
test "it doesn't trigger any query if collection_cache_versioning is enabled" do
with_collection_cache_versioning do
developers = Developer.where(name: "David")
assert_no_queries { developers.cache_key }
end
end
test "relation cache_key changes when the sql query changes" do
developers = Developer.where(name: "David")
other_relation = Developer.where(name: "David").where("1 = 1")