Always preload if using proc with multifetch cache#44616
Merged
byroot merged 1 commit intorails:mainfrom Aug 9, 2022
Merged
Conversation
05204db to
78bcb43
Compare
78bcb43 to
dc90451
Compare
rails#31250 optimised rendering a collection with `cached: true`, but also with `cached: proc {}`. The problem is the proc may reference associations that should be preloaded to avoid n+1s in generating the cache key. For example: ```ruby @posts = Post.preload(:author) @view.render partial: "test/partial", collection: @post, cached: proc { |post| [post, post.author] } ``` This will n+1 on `post.author`. To fix this, this PR will always preload the collection if there's a proc given for `cached`. `cached: true` will still not preload unless it renders, as it did in rails#31250.
dc90451 to
3f1f4c7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#31250 optimised rendering a collection with
cached: true, but also when using a proc:cached: proc {}. The problem is the proc may reference associations that should be preloaded to avoid n+1s in generating the cache key.For example:
Since #31250, this will perform n+1 queries on
post.author.preload(:author)is never executed.To fix this, this PR will always preload the collection if there's a proc given for
cached.cached: truewill still not preload unless it renders, as it did in #31250.