Fix #first(limit) to take advantage of #loaded? records if available#22053
Merged
Conversation
|
r? @chancancode (@rails-bot has picked a reviewer for you, use r? to override) |
first(limit) to take advantage of loaded? records if available
Contributor
Author
|
@chancancode Thoughts on this? |
Member
There was a problem hiding this comment.
You could move this outside the assert_queries block. Then we don't have to do the actual code 2 times and we can assert that no queries are executed.
Member
All uses of the `offset` are passing `offset_index`. Better to push down the `offset` consideration into `find_nth`. This also works toward enabling `find_nth_with_limit` to take advantage of the `loaded?` state of the relation.
I realized that `first(2)`, etc. was unnecessarily querying for the records when they were already preloaded. This was because `find_nth_with_limit` can not know which `@records` to return because it conflates the `offset` and `index` into a single variable, while the `@records` only needs the `index` itself to select the proper record. Because `find_nth` and `find_nth_with_limit` are public methods, I instead introduced a private method `find_nth_with_limit_and_offset` which is called internally and handles the `loaded?` checking. Once the `offset` argument is removed from `find_nth`, `find_nth_with_limit_and_offset` can be collapsed into `find_nth_with_limit`, with `offset` always equal to `offset_index`.
Contributor
Author
|
@senny Made the changes you suggested - note 2 questions:
|
Member
|
@Empact It's not necessary to make a PR for the removal. Once we release a new Major or Minor version we go through all deprecations and remove the ones that are due. Usually they stay for one release cycle. Meaning that the option will be deprecated in 5.0 and will be removed from 5.1. |
Member
senny
added a commit
that referenced
this pull request
Dec 28, 2015
Fix #first(limit) to take advantage of #loaded? records if available
Contributor
Author
|
Thanks @senny! 🎆 |
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.
I realized that
first(2), etc. were unnecessarily querying for therecords when they were already preloaded. This was because
find_nth_with_limitcan not know which@recordsto return becauseit conflates the
offsetandindexinto a single variable, whilethe
@recordsonly needs theindexitself to select the properrecord.
Because
find_nthandfind_nth_with_limitare public methods, Iinstead introduced a private method
find_nth_with_limit_and_offsetwhich is called internally and handles the
loaded?checking.Once the
offsetargument is removed fromfind_nth,find_nth_with_limit_and_offsetcan be collapsed intofind_nth_with_limit, withoffsetalways equal tooffset_index.