Skip to content
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

fix n+1 queries issue in supervisors#index and supervisors#datatable #5947

Merged
merged 6 commits into from
Aug 7, 2024

Conversation

guswhitten
Copy link
Contributor

What github issue is this PR for, if any?

Resolves #5890
Resolve #5891

What changed, and why?

Fixes the N+1 issues for the Supervisors#index and Supervisors#datatable. Does this by preloading any record associations that are used later on.

How is this tested? (please write tests!) 💖💪

Screenshots please :)

supervisors#index before
casa-5891-index-before

supervisors#index after
casa-5891-index-after

supervisors#datatable before
casa-5890-datatable-before

supervisors#datatable after
casa-5890-datatable-after

@github-actions github-actions bot added the ruby Pull requests that update Ruby code label Jul 27, 2024
@@ -12,7 +12,10 @@ class SupervisorsController < ApplicationController

def index
authorize Supervisor
@supervisors = policy_scope(current_organization.supervisors)
@supervisors = policy_scope(current_organization.supervisors).includes(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you know what this policy scope is adding to the query? could we move the includes? or is it reused elsewhere where the preloading isn't needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'm not sure about the policy_scope. i did not see other uses of @supervisors atm. is there a place you think itd be better to move it to?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not making a suggestion I was just posing another angle that you might consider, since it might be less code duplication.

You could move the includes bits to https://github.com/rubyforgood/casa/blob/main/app/policies/supervisor_policy.rb and have something like

class Scope < ApplicationPolicy::Scope
in there.

@github-actions github-actions bot added the erb label Aug 1, 2024
Copy link
Collaborator

@elasticspoon elasticspoon left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks! This was a 3 year old N+1 issue I believe :)

@elasticspoon elasticspoon merged commit 7407e04 into rubyforgood:main Aug 7, 2024
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
erb ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix SupervisorsController#datatable N+1 issues fix SupervisorsController#index N+1 issues
2 participants