Skip to content

Commit

Permalink
Merge pull request #38099 from alipman88/avoid_unecessary_query_if_ca…
Browse files Browse the repository at this point in the history
…che_versioning_enabled

Avoid unnecessary SQL query when calling ActiveRecord::Relation#cache_key
  • Loading branch information
eileencodes committed Jan 6, 2020
2 parents cd71826 + 76bb971 commit be40e8e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation.rb
Expand Up @@ -317,7 +317,7 @@ def compute_cache_key(timestamp_column = :updated_at) # :nodoc:
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)}"
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/collection_cache_key_test.rb
Expand Up @@ -113,6 +113,13 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
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")
Expand Down

0 comments on commit be40e8e

Please sign in to comment.