Defect #15058

Project authorization EnabledModule N+1 queries

Added by Felix B√ľnemann over 9 years ago. Updated over 9 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:-% Done:


Category:Code cleanup/refactoring
Target version:2.4.0
Resolution:Fixed Affected version:


There's a problem with project authorization that causes N+1 queries (one per project in @projects).

In ApplicationController#authorize redmine checks authorization on @project or @projects. This triggers the code in Project#allowed_permissions which calls enabled_modules.pluck(:name).

The problem here is that pluck circumvents preloading, so no matter wether we do @projects = Project.includes(:enabled_modules).where(conditions) in a controller, it will still trigger one extra query per project.

Given that the EnableModule model is tiny, this could be prevented by using instead.

Associated revisions

Revision 12228
Added by Jean-Philippe Lang over 9 years ago

Avoid a query if enabled_modules association is already loaded (#15058).


#1 Updated by Jean-Philippe Lang over 9 years ago

  • Category changed from Database to Code cleanup/refactoring
  • Status changed from New to Closed
  • Resolution set to Fixed

Fixed in r12228, #pluck won't be called is the association is preloaded.

#2 Updated by Jean-Philippe Lang over 9 years ago

  • Target version set to 2.4.0

#3 Updated by Felix B√ľnemann over 9 years ago

Nice solution, I didn't know about loaded? until now.

Also available in: Atom PDF