Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7849093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:25:33+00:00 2026-06-02T18:25:33+00:00

I am trying to speed up my app by eager loading some sql calls.

  • 0

I am trying to speed up my app by eager loading some sql calls. I am using the CanCan gem to handle my admin authorization. I have a Roles table with three different roles and a many-to-many table roles_users. Each time a page loads with a CanCan ability set it does three separate sql queries.

 Role Load (0.6ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id"    
= "roles_users"."role_id" WHERE "roles_users"."user_id" = 2 AND "roles"."name" = 'admin'  
  LIMIT 1
Role Load (0.4ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" 
  = "roles_users"."role_id" WHERE "roles_users"."user_id" = 2 AND "roles"."name" =  
 'manager' LIMIT 1
 Role Load (0.3ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" 
  = "roles_users"."role_id" WHERE "roles_users"."user_id" = 2 AND "roles"."name" = 'user' 
  LIMIT 1 

I have tried putting a default_scope :include => :roles in the User class and also putting :includes in the has_and_belongs_to_many calls.

Where can I eager load the Roles table to use only 1 SQL query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:25:36+00:00Added an answer on June 2, 2026 at 6:25 pm

    Looks like you might have some code that looks like the below. Namely, it takes a string or symbol that you need to compare to some value in a role record, and is not the role itself. So look for something like this:

    def do_i_have_role(role_name_to_check)
      self.roles.detect do |role|
        role.name == role_name_to_check
      end
    end
    

    You could do some eager loading here to fix it up, but another way to skin this cat is to restructure your query. Namely, look for the role object first, then see if your account has that role.

    def do_i_have_role(role_name_to_check)
      role = Role.where(:name => role_name_to_check)
      self.roles.include? role
    end
    

    Now it’s just one hit to the roles database instead of 3 (and a hit to the accounts table and one more hit to the account_user table which you will probably never be able to avoid).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to speed up my app by loading certain DLLs into the
I was trying to speed up some code, and then I tried compiling a
I am trying to speed up an often used query. Using a CompiledQuery seemed
I'm trying to speed up a large RSpec project's tests. In addition to using
I'm trying to set up a rails 3.2 app with the new compass-rails gem
I'm trying to get the basics of iOS programming down. I have an app
I'm trying to speed up the start times for my Direct3D app a bit,
I'm trying to speed up rspecing in a rails 2.3.8 app with spork. When
I am trying to backfit my app (already on the AppStore with customers using
I am currently trying to make a speed dial app for various numbers the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.