Rails 5.2 cache key for relations generating two queries
up vote
1
down vote
favorite
I have a view using fragment caching for an ActiveRecord
relation, e.g.
<% cache MyModel.all do %>
...
<% end %>
I see two DB queries generated in this case
SELECT COUNT(*) AS "size", MAX("my_model"."updated_at") AS timestamp FROM "my_model"
SELECT "my_model".* from "my_model"
I expect the first one, and it's usually a much more efficient query. I did not expect the second one.
If I instead use:
<% cache ActiveSupport::Cache.expand_cache_key(MyModel.all) do %>
...
<% end %>
then I get only the first query with the same resulting cache key.
Is it a bug or am I doing something wrong?
EDIT: narrowed down to where this happens: see https://github.com/rails/rails/pull/29092#issuecomment-437572543
when
normalize_version
is executed, the relation does not respond tocache_version
, and
therefore ends up being expanded with
to_a
.
So essentially, callingProduct.all.to_a
and then for each object
callingcache_version
, which returnsnil
.
ruby-on-rails caching activerecord activesupport
add a comment |
up vote
1
down vote
favorite
I have a view using fragment caching for an ActiveRecord
relation, e.g.
<% cache MyModel.all do %>
...
<% end %>
I see two DB queries generated in this case
SELECT COUNT(*) AS "size", MAX("my_model"."updated_at") AS timestamp FROM "my_model"
SELECT "my_model".* from "my_model"
I expect the first one, and it's usually a much more efficient query. I did not expect the second one.
If I instead use:
<% cache ActiveSupport::Cache.expand_cache_key(MyModel.all) do %>
...
<% end %>
then I get only the first query with the same resulting cache key.
Is it a bug or am I doing something wrong?
EDIT: narrowed down to where this happens: see https://github.com/rails/rails/pull/29092#issuecomment-437572543
when
normalize_version
is executed, the relation does not respond tocache_version
, and
therefore ends up being expanded with
to_a
.
So essentially, callingProduct.all.to_a
and then for each object
callingcache_version
, which returnsnil
.
ruby-on-rails caching activerecord activesupport
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a view using fragment caching for an ActiveRecord
relation, e.g.
<% cache MyModel.all do %>
...
<% end %>
I see two DB queries generated in this case
SELECT COUNT(*) AS "size", MAX("my_model"."updated_at") AS timestamp FROM "my_model"
SELECT "my_model".* from "my_model"
I expect the first one, and it's usually a much more efficient query. I did not expect the second one.
If I instead use:
<% cache ActiveSupport::Cache.expand_cache_key(MyModel.all) do %>
...
<% end %>
then I get only the first query with the same resulting cache key.
Is it a bug or am I doing something wrong?
EDIT: narrowed down to where this happens: see https://github.com/rails/rails/pull/29092#issuecomment-437572543
when
normalize_version
is executed, the relation does not respond tocache_version
, and
therefore ends up being expanded with
to_a
.
So essentially, callingProduct.all.to_a
and then for each object
callingcache_version
, which returnsnil
.
ruby-on-rails caching activerecord activesupport
I have a view using fragment caching for an ActiveRecord
relation, e.g.
<% cache MyModel.all do %>
...
<% end %>
I see two DB queries generated in this case
SELECT COUNT(*) AS "size", MAX("my_model"."updated_at") AS timestamp FROM "my_model"
SELECT "my_model".* from "my_model"
I expect the first one, and it's usually a much more efficient query. I did not expect the second one.
If I instead use:
<% cache ActiveSupport::Cache.expand_cache_key(MyModel.all) do %>
...
<% end %>
then I get only the first query with the same resulting cache key.
Is it a bug or am I doing something wrong?
EDIT: narrowed down to where this happens: see https://github.com/rails/rails/pull/29092#issuecomment-437572543
when
normalize_version
is executed, the relation does not respond tocache_version
, and
therefore ends up being expanded with
to_a
.
So essentially, callingProduct.all.to_a
and then for each object
callingcache_version
, which returnsnil
.
ruby-on-rails caching activerecord activesupport
ruby-on-rails caching activerecord activesupport
edited Nov 10 at 10:08
asked Nov 9 at 8:59
gingerlime
2,99932456
2,99932456
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222582%2frails-5-2-cache-key-for-relations-generating-two-queries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown