Skip to content


Subversion checkout URL

You can clone with
Download ZIP


CanCan fires a redundant query when using pagination #778

abitdodgy opened this Issue · 1 comment

2 participants


CanCan fires an extra query when using load_and_authorize_resource along with a pagination. Is this by design?

load_and_authorize_resource :group
load_and_authorize_resource :event, through: :group

def index
  @events = @events.paginate(...)

Notice the first Event Load query without, which does not include pagination params. Then notice the second Event Load query, which does include pagination params:

Group Load (0.1ms)  SELECT "groups".* FROM "groups" WHERE "groups"."id" = ? LIMIT 1  [["id", "1"]]
Event Load (0.2ms)  SELECT "events".* FROM "events" WHERE "events"."group_id" = 1
  Rendered ...
    (0.1ms)  SELECT COUNT(*) FROM "events" WHERE "events"."group_id" = 1
Event Load (0.1ms)  SELECT "events".* FROM "events" WHERE "events"."group_id" = 1 LIMIT 10 OFFSET 0
  CACHE (0.0ms)  SELECT COUNT(*) FROM "events" WHERE "events"."group_id" = 1

This is be bad news if a group has thousands of events.

Removing CanCan removes the first query, which is redundant. We end up with one Event Load query and it includes pagination params. I thought CanCan builds the query first then fires it.

Is this a bug or is it by design?


Same issue here. Did you do any extra research on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.