Skip to content

Loaded associations should not run a new query when size is called#32617

Merged
kamipo merged 1 commit into
rails:masterfrom
tgturner:size-should-use-available-association
Apr 27, 2018
Merged

Loaded associations should not run a new query when size is called#32617
kamipo merged 1 commit into
rails:masterfrom
tgturner:size-should-use-available-association

Conversation

@tgturner

Copy link
Copy Markdown
Contributor

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

@rails-bot

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor

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?.

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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.

@tgturner tgturner force-pushed the size-should-use-available-association branch from 876a6ff to 45f566d Compare April 21, 2018 15:48
@tgturner

Copy link
Copy Markdown
Contributor Author

@chancancode I don't think the !find_target check in empty? is necessary. I added all relevant test cases I could think of.

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 !find_target. Please let me know if any of my assumptions are incorrect, and thanks for working on this with me!

@chancancode

Copy link
Copy Markdown
Member

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.

@tgturner

Copy link
Copy Markdown
Contributor Author

Thanks for following up!

@chancancode chancancode assigned pixeltrix and kamipo and unassigned kaspth Apr 24, 2018
Comment thread activerecord/CHANGELOG.md Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/empty/empty?/

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_empty post.readers

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_no_queries do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_no_queries do

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

@tgturner tgturner force-pushed the size-should-use-available-association branch from 45f566d to d061eb4 Compare April 26, 2018 15:50
@tgturner

Copy link
Copy Markdown
Contributor Author

@kamipo Thank you for the feedback! Updated PR based on your comments and rebased on top of Master

@kamipo kamipo left a comment

Copy link
Copy Markdown
Member

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
@tgturner tgturner force-pushed the size-should-use-available-association branch from d061eb4 to b07b970 Compare April 26, 2018 16:41
@tgturner

Copy link
Copy Markdown
Contributor Author

Done!

@kamipo kamipo merged commit 0a8fe15 into rails:master Apr 27, 2018
@chancancode

Copy link
Copy Markdown
Member

Thank you @tgturner and everyone else who reviewed the PR! ❤️

@tgturner

Copy link
Copy Markdown
Contributor Author

Thanks to everyone that helped!

@tgturner tgturner deleted the size-should-use-available-association branch April 27, 2018 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants