New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loaded associations should not run a new query when size is called #32617
Loaded associations should not run a new query when size is called #32617
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
@@ -231,7 +233,7 @@ def size | |||
# loaded and you are going to fetch the records anyway it is better to | |||
# check <tt>collection.length.zero?</tt>. | |||
def empty? | |||
if loaded? | |||
if loaded? || @association_ids |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We talked about this briefly in the issue and think this should probably have the !find_target?
check as well. Can you investigate why that may matter, and if it does, add a test for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need the !find_target?
since that will be implicit if @association_ids
is defined because it calls size.zero?
.
car.bulbs.size | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for empty? as well?
@@ -212,6 +212,8 @@ def destroy(*records) | |||
def size | |||
if !find_target? || loaded? | |||
target.size | |||
elsif @association_ids | |||
@association_ids.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rebase on current master?
This branch will break existing behavior described at e76e477 since ids_reader
doesn't respect dirty target when the target is not loaded yet.
876a6ff
to
45f566d
Compare
@chancancode I don't think the Thanks to @kamipo, I noticed that the changes I made were causing incorrect results to be returned for models with unsaved associations. Handling that case, I believe, handled the need for |
This seems good to me but I would love if @pixeltrix or @kamipo can double check the more subtle aspects of this as I am not super familiar with them, so I might have missed some things. |
Thanks for following up! |
activerecord/CHANGELOG.md
Outdated
@@ -1,3 +1,8 @@ | |||
* Ensure `Associations::CollectionAssociation#size` and `Associations::CollectionAssociation#empty` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/empty/empty?/
@@ -212,6 +212,8 @@ def destroy(*records) | |||
def size | |||
if !find_target? || loaded? | |||
target.size | |||
elsif @association_ids && target.empty? | |||
@association_ids.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer @association_ids.size
since it is an only place using length
in the class.
def test_collection_empty_with_dirty_target | ||
post = posts(:thinking) | ||
assert_equal [], post.reader_ids | ||
assert_equal true, post.readers.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_empty post.readers
post.readers.reset | ||
post.readers.build | ||
assert_equal [], post.reader_ids | ||
assert_equal false, post.readers.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_not_empty post.readers
car_two = Car.create! | ||
car_two.bulb_ids | ||
|
||
assert_queries(0) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_no_queries do
assert_equal false, car.bulbs.empty? | ||
end | ||
|
||
assert_queries(0) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_no_queries do
car_two = Car.create! | ||
|
||
assert_queries(1) do | ||
assert_equal false, car.bulbs.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_not_empty car.bulbs
end | ||
|
||
assert_queries(1) do | ||
assert_equal true, car_two.bulbs.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_empty car_two.bulbs
car_two.bulb_ids | ||
|
||
assert_queries(0) do | ||
assert_equal false, car.bulbs.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_not_empty car.bulbs
end | ||
|
||
assert_queries(0) do | ||
assert_equal true, car_two.bulbs.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_empty car_two.bulbs
45f566d
to
d061eb4
Compare
@kamipo Thank you for the feedback! Updated PR based on your comments and rebased on top of Master |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Can you squash your commits into one?
Already loaded associations were running an extra query when `size` was called on the association. This fix ensures that an extra query is no longer run. Update tests to use proper methods
d061eb4
to
b07b970
Compare
Done! |
Thank you @tgturner and everyone else who reviewed the PR! |
Thanks to everyone that helped! |
Already loaded associations were running an extra query when
size
was called on the association.This fix ensures that an extra query is no longer run.
This addresses #32569