From 586c6f4b4c48d672c3e8454139ba2465d97f92fb Mon Sep 17 00:00:00 2001 From: Nelson Jovel Date: Thu, 21 Dec 2023 13:34:16 -0800 Subject: [PATCH] fix: reduce number of n+1 queries --- app/controllers/overview_controller.rb | 8 ++++++-- app/models/category.rb | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/overview_controller.rb b/app/controllers/overview_controller.rb index 91d761ab..8e78cd25 100644 --- a/app/controllers/overview_controller.rb +++ b/app/controllers/overview_controller.rb @@ -6,7 +6,7 @@ class OverviewController < SqmApplicationController def index @variance_chart_row_presenters = measures.map(&method(:presenter_for_measure)) - @category_presenters = Category.sorted.map { |category| CategoryPresenter.new(category:) } + @category_presenters = categories.map { |category| CategoryPresenter.new(category:) } @student_response_rate_presenter = ResponseRatePresenter.new(focus: :student, school: @school, academic_year: @academic_year) @teacher_response_rate_presenter = ResponseRatePresenter.new(focus: :teacher, school: @school, @@ -33,6 +33,10 @@ class OverviewController < SqmApplicationController end def subcategories - @subcategories ||= Subcategory.all.includes(%i[measures admin_data_items category]) + @subcategories ||= categories.flat_map(&:subcategories) + end + + def categories + @categories ||= Category.sorted.includes(%i[measures admin_data_items subcategories]) end end diff --git a/app/models/category.rb b/app/models/category.rb index 836975af..318d3f5f 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -8,4 +8,5 @@ class Category < ActiveRecord::Base has_many :subcategories has_many :measures, through: :subcategories + has_many :admin_data_items, through: :measures end