From f7dd189435cbd42306dbc5987b02cf82c6fc9223 Mon Sep 17 00:00:00 2001 From: rsmokeUM Date: Mon, 16 Feb 2026 14:47:17 -0500 Subject: [PATCH 1/2] Enhance entry loading in JudgeDashboard views for improved data retrieval - Updated the available entries partial to include associated entry files and categories, optimizing data loading for better performance. - Modified the selected entries partial to include category and entry file attachments in the EntryRanking query, ensuring all necessary data is preloaded. - Refined the JudgeDashboardController to include categories and entry file attachments in the entries query, reducing N+1 query issues and improving overall efficiency. These changes enhance the user experience by providing richer data in the dashboard while optimizing database interactions. --- app/controllers/judge_dashboard_controller.rb | 2 +- app/views/judge_dashboard/_available_entries.html.erb | 2 ++ app/views/judge_dashboard/_selected_entries.html.erb | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) 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| %> From 958ebc527dca22230f6aa19102dc845f6ea0e175 Mon Sep 17 00:00:00 2001 From: rsmokeUM Date: Mon, 16 Feb 2026 14:49:47 -0500 Subject: [PATCH 2/2] Add test for eager loading of entry file attachments in JudgeDashboardController - Introduced a new test case to verify that entry file attachments are eager loaded in the index action, reducing N+1 query issues. - The test ensures that the number of SQL queries for attachments does not exceed two, optimizing performance during data retrieval. These changes enhance the test coverage for the JudgeDashboardController, ensuring efficient loading of associated data. --- .../judge_dashboard_controller_spec.rb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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)