diff --git a/app/controllers/judge_dashboard_controller.rb b/app/controllers/judge_dashboard_controller.rb
index 20d6711a..dc0ba50b 100644
--- a/app/controllers/judge_dashboard_controller.rb
+++ b/app/controllers/judge_dashboard_controller.rb
@@ -18,7 +18,7 @@ def index
.includes(contest_instance: [
contest_description: [ :container ],
judging_rounds: [ :round_judge_assignments ],
- entries: [ :entry_rankings ]
+ entries: [ :entry_rankings, :category, { entry_file_attachment: :blob } ]
])
.where(judging_assignments: { active: true })
.where(contest_instances: { active: true, id: contest_instances_with_incomplete_assigned_rounds })
diff --git a/app/views/judge_dashboard/_available_entries.html.erb b/app/views/judge_dashboard/_available_entries.html.erb
index 2c2745c4..31fac86e 100644
--- a/app/views/judge_dashboard/_available_entries.html.erb
+++ b/app/views/judge_dashboard/_available_entries.html.erb
@@ -10,6 +10,8 @@
).pluck(:entry_id) %>
<% assignment.contest_instance.current_round_entries
+ .with_attached_entry_file
+ .includes(:category)
.where.not(id: ranked_entry_ids)
.each do |entry| %>
<%= turbo_frame_tag dom_id(entry) do %>
diff --git a/app/views/judge_dashboard/_selected_entries.html.erb b/app/views/judge_dashboard/_selected_entries.html.erb
index e6852d9c..bfc4bb08 100644
--- a/app/views/judge_dashboard/_selected_entries.html.erb
+++ b/app/views/judge_dashboard/_selected_entries.html.erb
@@ -13,7 +13,7 @@
Please select and rank <%= assignment.contest_instance.current_judging_round.required_entries_count %> entries
- <% EntryRanking.includes(:entry)
+ <% EntryRanking.includes(entry: [ :category, { entry_file_attachment: :blob } ])
.where(judging_round: assignment.contest_instance.current_judging_round, user: current_user)
.order(:rank)
.each do |ranking| %>
diff --git a/spec/controllers/judge_dashboard_controller_spec.rb b/spec/controllers/judge_dashboard_controller_spec.rb
index ea6e554a..b4599a88 100644
--- a/spec/controllers/judge_dashboard_controller_spec.rb
+++ b/spec/controllers/judge_dashboard_controller_spec.rb
@@ -61,6 +61,28 @@
end
end
+ context 'when judge is assigned and index renders entries with attachments' do
+ before do
+ create(:judging_assignment, user: judge, contest_instance: contest_instance, active: true)
+ create(:round_judge_assignment, user: judge, judging_round: round_1, active: true)
+ create_list(:entry, 3, contest_instance: contest_instance, deleted: false)
+ end
+
+ it 'eager loads entry_file attachments to avoid N+1 queries' do
+ attachment_queries = 0
+ counter = ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload|
+ attachment_queries += 1 if payload[:sql] =~ /active_storage_attachments/
+ end
+
+ get :index
+
+ ActiveSupport::Notifications.unsubscribe(counter)
+
+ # With eager loading we should have at most 2 queries (attachments + blobs), not one per entry
+ expect(attachment_queries).to be <= 2
+ end
+ end
+
context 'when judge is assigned to contest but no rounds' do
before do
create(:judging_assignment, user: judge, contest_instance: contest_instance, active: true)