From 239d46d5697a227e6ad3625a501fbcc803dddc08 Mon Sep 17 00:00:00 2001 From: James Rose Date: Thu, 11 May 2023 13:51:53 +0100 Subject: [PATCH] Fix N+1 query in scheme answer options page (#1614) --- app/models/form/lettings/questions/scheme_id.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/form/lettings/questions/scheme_id.rb b/app/models/form/lettings/questions/scheme_id.rb index 487b1b2c4..20bda8412 100644 --- a/app/models/form/lettings/questions/scheme_id.rb +++ b/app/models/form/lettings/questions/scheme_id.rb @@ -30,10 +30,10 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question def displayed_answer_options(lettings_log, _user = nil) organisation = lettings_log.owning_organisation || lettings_log.created_by&.organisation schemes = if organisation - Scheme.select(:id).where(owning_organisation_id: organisation.id, - confirmed: true) + Scheme.includes(:locations).select(:id).where(owning_organisation_id: organisation.id, + confirmed: true) else - Scheme.select(:id).where(confirmed: true) + Scheme.includes(:locations).select(:id).where(confirmed: true) end filtered_scheme_ids = schemes.joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", Time.zone.today)).map(&:id)