Avoid N+1 SQL queries with CanCanCan accessibility checks
This commit is contained in:
parent
f154c1838c
commit
b36a49a6cd
6 changed files with 14 additions and 12 deletions
|
|
@ -288,8 +288,9 @@ class PackagesController < ApplicationController
|
|||
|
||||
# Return packages matching the criteria given by parameters
|
||||
def query_packages
|
||||
# Eager load the screenshots to avoid N+1 queries to check Screenshot.accessibly_by later
|
||||
packages = Package.includes(:screenshots).order(visits: :desc)
|
||||
|
||||
|
||||
# text search
|
||||
if params[:search].present?
|
||||
logger.debug "Searching for #{params[:search]}"
|
||||
|
|
@ -298,13 +299,14 @@ class PackagesController < ApplicationController
|
|||
|
||||
case params[:show]
|
||||
when 'with'
|
||||
packages = packages.with_screenshots
|
||||
# Enrich the result with the screenshots readable by the current user (CanCanCan)
|
||||
packages = packages.where(screenshots: Screenshot.accessible_by(current_ability, :view))
|
||||
logger.debug 'Limiting packages to those with screenshots'
|
||||
when 'without'
|
||||
packages = packages.without_screenshots
|
||||
logger.debug 'Limiting packages to those without screenshots'
|
||||
end
|
||||
|
||||
|
||||
return packages
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue