Skip to content

Commit 00754a3

Browse files
committed
Avoid preloading triages
This avoids potential issues where preloading large amounts of data can be slow and require a lot of memory. Instead, where N+1 issues isn't a problem, it's fine to run an additional query to the database and fetch only the data we need as is the case in most situations.
1 parent 6332bd9 commit 00754a3

File tree

8 files changed

+34
-23
lines changed

8 files changed

+34
-23
lines changed

app/components/app_outcome_banner_component.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ def vaccination_record
6363
end
6464

6565
def triage
66-
@triage ||= patient.latest_triage(programme:)
66+
@triage ||=
67+
patient
68+
.triages
69+
.not_invalidated
70+
.includes(:performed_by)
71+
.order(created_at: :desc)
72+
.find_by(programme:)
6773
end
6874

6975
def session_attendance

app/components/app_triage_form_component.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@ def initialize(
1313

1414
@patient_session = patient_session
1515
@programme = programme
16-
@triage =
17-
triage ||
18-
Triage.new.tap do |t|
19-
if (latest_triage = patient_session.patient.latest_triage(programme:))
20-
t.status = latest_triage.status
21-
end
22-
end
16+
@triage = triage || default_triage
2317
@url = url
2418
@method = method
2519
@legend = legend
2620
end
2721

2822
private
2923

30-
attr_reader :programme
24+
attr_reader :patient_session, :programme
25+
26+
delegate :patient, to: :patient_session
3127

3228
def fieldset_options
3329
text = "Is it safe to vaccinate #{@patient_session.patient.given_name}?"
@@ -41,4 +37,15 @@ def fieldset_options
4137
{ legend: { text:, size: "s", class: "app-fieldset__legend--reset" } }
4238
end
4339
end
40+
41+
def default_triage
42+
previous_triage =
43+
patient
44+
.triages
45+
.not_invalidated
46+
.order(created_at: :desc)
47+
.find_by(programme:)
48+
49+
Triage.new(status: previous_triage&.status)
50+
end
4451
end

app/controllers/patients_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def set_patient
7171
:gp_practice,
7272
:organisation,
7373
:school,
74-
:triages,
7574
consents: %i[parent patient],
7675
parent_relationships: :parent,
7776
patient_sessions: %i[location session_attendances]

app/jobs/school_session_reminders_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def perform
99
patient_sessions =
1010
PatientSession
1111
.includes(
12-
patient: [:parents, :triages, { consents: %i[parent patient] }],
12+
patient: [:parents, { consents: %i[parent patient] }],
1313
session: :programmes
1414
)
1515
.eager_load(:session)

app/models/patient.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Patient < ApplicationRecord
7373
has_many :school_moves
7474
has_many :session_notifications
7575
has_many :triage_statuses
76-
has_many :triages, -> { order(:created_at) }
76+
has_many :triages
7777
has_many :vaccination_records, -> { kept }
7878
has_many :vaccination_statuses
7979

@@ -277,13 +277,6 @@ def triage_status(programme:)
277277
triage_statuses.build(programme:)
278278
end
279279

280-
def latest_triage(programme:)
281-
triages
282-
.select { it.programme_id == programme.id }
283-
.reject(&:invalidated?)
284-
.max_by(&:created_at)
285-
end
286-
287280
def vaccination_status(programme:)
288281
# Use `find` to allow for preloading.
289282
vaccination_statuses.find { it.programme_id == programme.id } ||

app/models/patient/triage_status.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class Patient::TriageStatus < ApplicationRecord
2929
end,
3030
through: :patient
3131

32-
has_many :triages, -> { not_invalidated }, through: :patient
32+
has_many :triages,
33+
-> { not_invalidated.order(created_at: :desc) },
34+
through: :patient
3335

3436
has_many :vaccination_records,
3537
-> { kept.order(performed_at: :desc) },
@@ -95,6 +97,6 @@ def status_should_be_required?
9597
end
9698

9799
def latest_triage
98-
@latest_triage ||= triages.select { it.programme_id == programme_id }.last
100+
@latest_triage ||= triages.find { it.programme_id == programme_id }
99101
end
100102
end

app/models/patient/vaccination_status.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class Patient::VaccinationStatus < ApplicationRecord
2929
end,
3030
through: :patient
3131

32-
has_many :triages, -> { not_invalidated }, through: :patient
32+
has_many :triages,
33+
-> { not_invalidated.order(created_at: :desc) },
34+
through: :patient
3335

3436
has_many :vaccination_records,
3537
-> { kept.order(performed_at: :desc) },

app/models/patient_session/vaccination_status.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class PatientSession::VaccinationStatus < ApplicationRecord
3131
end,
3232
through: :patient
3333

34-
has_many :triages, -> { not_invalidated }, through: :patient
34+
has_many :triages,
35+
-> { not_invalidated.order(created_at: :desc) },
36+
through: :patient
3537

3638
has_many :vaccination_records,
3739
-> { kept.order(performed_at: :desc) },

0 commit comments

Comments
 (0)