Skip to content

Commit

Permalink
Simplify set_patients query
Browse files Browse the repository at this point in the history
Instead of arel and a Ruby based .sort_by, we can use native AR queries
and .order.
  • Loading branch information
tvararu committed Sep 25, 2024
1 parent 0d33151 commit 2d32cb2
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions app/controllers/sessions/edit_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2d32cb2

Please sign in to comment.