From 77e2b1e203988f5141987ebbfe1804d8783bdd6d Mon Sep 17 00:00:00 2001 From: rebuilt Date: Mon, 28 Aug 2023 13:46:23 -0700 Subject: [PATCH] fix: I broke the feature where the site would automatically navigate the user to the latest year that had sufficient data when I made response rates calculate on each page load instead of being precalculated and stored in the database. Instead of a database lookup for response rates that meet the sufficiency threshold, I caculate the latest year when a user chooses a school from the welcome page. --- app/controllers/home_controller.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 62232582..ec44fdd6 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -43,12 +43,15 @@ class HomeController < ApplicationController end def year - latest_response_rate = ResponseRate.where(school:) - .where('meets_student_threshold = ? or meets_teacher_threshold = ?', true, true) - .joins('inner join academic_years a on response_rates.academic_year_id=a.id') - .order('a.range DESC').first - academic_year = latest_response_rate.academic_year.range if latest_response_rate.present? + return nil unless school.present? - academic_year || AcademicYear.order('range DESC').first.range + academic_year = AcademicYear.all.order(range: :DESC).find do |ay| + Subcategory.all.any? do |subcategory| + rate = subcategory.response_rate(school:, academic_year: ay) + rate.meets_student_threshold || rate.meets_teacher_threshold + end + end + + academic_year.range || AcademicYear.order("range DESC").first.range end end