From 2d32cb2c0efff956973a4d3d0551ed47fa992ca1 Mon Sep 17 00:00:00 2001 From: Theodor Vararu Date: Tue, 24 Sep 2024 14:20:42 +0300 Subject: [PATCH] Simplify set_patients query Instead of arel and a Ruby based .sort_by, we can use native AR queries and .order. --- app/controllers/sessions/edit_controller.rb | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/app/controllers/sessions/edit_controller.rb b/app/controllers/sessions/edit_controller.rb index 64a1d6c22..c60c0a3b7 100644 --- a/app/controllers/sessions/edit_controller.rb +++ b/app/controllers/sessions/edit_controller.rb @@ -104,20 +104,17 @@ def set_patients # Get a list of patients but ensure we don't include patients that are # already in other sessions, if those sessions are active (not draft) and # for the same vaccine/programme. + patients_in_active_sessions_and_same_team = + Patient + .joins(:patient_sessions) + .joins(:sessions) + .where(sessions: { active: true, team: @session.team }) @patients = @session .location .patients - .where.not( - Session - .joins(:patient_sessions) - .active - .where(team: @session.team) - .where("patient_sessions.patient_id = patients.id") - .arel - .exists - ) - .sort_by(&:last_name) + .where.not(id: patients_in_active_sessions_and_same_team) + .order(:last_name) end def validate_params