From 8a8bd4846cb25a445d77b27282c373813ed946a5 Mon Sep 17 00:00:00 2001 From: Nelson Jovel Date: Wed, 31 Jan 2024 15:06:16 -0800 Subject: [PATCH] chore: show all disaggregations without error on analyze page --- app/models/dashboard/race.rb | 5 +++-- app/models/dashboard/student.rb | 5 ++--- app/models/dashboard/student_race.rb | 4 ++-- app/models/dashboard/survey_item_response.rb | 4 ++-- .../analyze/graph/column/race_column/race_count.rb | 4 ++-- .../dashboard/analyze/graph/column/score_for_race.rb | 4 ++-- .../dashboard/survey_responses_data_loader.rb | 11 +++++++---- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/app/models/dashboard/race.rb b/app/models/dashboard/race.rb index 3f78ff4..a17d60b 100644 --- a/app/models/dashboard/race.rb +++ b/app/models/dashboard/race.rb @@ -1,8 +1,9 @@ module Dashboard class Race < ApplicationRecord include FriendlyId - has_and_belongs_to_many :students, join_table: :dashboard_student_races, class_name: "Student", - foreign_key: :dashboard_student_id, association_foreign_key: :dashboard_student_id + + has_many :student_races, class_name: "StudentRace", foreign_key: :dashboard_race_id + has_many :students, through: :student_races, foreign_key: :dashboard_student_id friendly_id :designation, use: [:slugged] diff --git a/app/models/dashboard/student.rb b/app/models/dashboard/student.rb index 44102f3..b7d32a2 100644 --- a/app/models/dashboard/student.rb +++ b/app/models/dashboard/student.rb @@ -1,8 +1,7 @@ module Dashboard class Student < ApplicationRecord - # has_many :dashboard_survey_item_responses - has_and_belongs_to_many :races, join_table: :dashboard_student_races, class_name: "Race", - foreign_key: :dashboard_race_id, association_foreign_key: :dashboard_race_id + has_many :student_races, class_name: "StudentRace", foreign_key: :dashboard_student_id + has_many :races, through: :student_races, foreign_key: :dashboard_race_id encrypts :lasid, deterministic: true end diff --git a/app/models/dashboard/student_race.rb b/app/models/dashboard/student_race.rb index 39f3d18..c66baa0 100644 --- a/app/models/dashboard/student_race.rb +++ b/app/models/dashboard/student_race.rb @@ -1,6 +1,6 @@ module Dashboard class StudentRace < ApplicationRecord - belongs_to :dashboard_student - belongs_to :dashboard_race + belongs_to :student, optional: true, class_name: "Student", foreign_key: :dashboard_student_id + belongs_to :race, optional: true, class_name: "Race", foreign_key: :dashboard_race_id end end diff --git a/app/models/dashboard/survey_item_response.rb b/app/models/dashboard/survey_item_response.rb index 28e9239..eb0c6df 100644 --- a/app/models/dashboard/survey_item_response.rb +++ b/app/models/dashboard/survey_item_response.rb @@ -46,9 +46,9 @@ module Dashboard } scope :averages_for_race, lambda { |school, academic_year, race| - SurveyItemResponse.joins("JOIN student_races on survey_item_responses.student_id = student_races.student_id JOIN students on students.id = student_races.student_id").where( + SurveyItemResponse.joins("JOIN dashboard_student_races on dashboard_survey_item_responses.dashboard_student_id = dashboard_student_races.dashboard_student_id JOIN dashboard_students on dashboard_students.id = dashboard_student_races.dashboard_student_id").where( school:, academic_year:, grade: school.grades(academic_year:) - ).where("student_races.race_id": race.id).group(:survey_item_id).having("count(*) >= 10").average(:likert_score) + ).where("dashboard_student_races.dashboard_race_id": race.id).group(:dashboard_survey_item_id).having("count(*) >= 10").average(:likert_score) } def self.grouped_responses(school:, academic_year:) diff --git a/app/presenters/dashboard/analyze/graph/column/race_column/race_count.rb b/app/presenters/dashboard/analyze/graph/column/race_column/race_count.rb index 70acc40..489ba08 100644 --- a/app/presenters/dashboard/analyze/graph/column/race_column/race_count.rb +++ b/app/presenters/dashboard/analyze/graph/column/race_column/race_count.rb @@ -11,10 +11,10 @@ module Dashboard end def n_size(year_index) - SurveyItemResponse.joins("JOIN student_races on survey_item_responses.student_id = student_races.student_id JOIN students on students.id = student_races.student_id").where( + SurveyItemResponse.joins("JOIN dashboard_student_races on dashboard_survey_item_responses.dashboard_student_id = dashboard_student_races.dashboard_student_id JOIN dashboard_students on dashboard_students.id = dashboard_student_races.dashboard_student_id").where( school:, academic_year: academic_years[year_index], survey_item: measure.student_survey_items - ).where("student_races.race_id": race.id).select(:response_id).distinct.count + ).where("dashboard_student_races.dashboard_race_id": race.id).select(:response_id).distinct.count end end end diff --git a/app/presenters/dashboard/analyze/graph/column/score_for_race.rb b/app/presenters/dashboard/analyze/graph/column/score_for_race.rb index d0585c7..ad90a8a 100644 --- a/app/presenters/dashboard/analyze/graph/column/score_for_race.rb +++ b/app/presenters/dashboard/analyze/graph/column/score_for_race.rb @@ -32,9 +32,9 @@ module Dashboard def sufficient_student_responses?(academic_year:) return false unless measure.subcategory.response_rate(school:, academic_year:).meets_student_threshold? - number_of_students_for_a_racial_group = SurveyItemResponse.joins("JOIN student_races on dashboard_survey_item_responses.student_id = student_races.student_id JOIN students on students.id = student_races.student_id").where( + number_of_students_for_a_racial_group = SurveyItemResponse.joins("JOIN dashboard_student_races on dashboard_survey_item_responses.dashboard_student_id = dashboard_student_races.dashboard_student_id JOIN dashboard_students on dashboard_students.id = dashboard_student_races.dashboard_student_id").where( school:, academic_year: - ).where("student_races.race_id": race.id).distinct.pluck(:student_id).count + ).where("dashboard_student_races.dashboard_race_id": race.id).distinct.pluck(:dashboard_student_id).count number_of_students_for_a_racial_group >= 10 end end diff --git a/app/services/dashboard/survey_responses_data_loader.rb b/app/services/dashboard/survey_responses_data_loader.rb index e29bf2d..7271459 100644 --- a/app/services/dashboard/survey_responses_data_loader.rb +++ b/app/services/dashboard/survey_responses_data_loader.rb @@ -87,7 +87,9 @@ module Dashboard student = Student.find_or_create_by(response_id: row.response_id, lasid: row.lasid) student.races.delete_all tmp_races = row.races.map { |race| races[race] } - student.races += tmp_races + tmp_races.each do |race| + student.races << race + end student.save row.survey_items.map do |survey_item| @@ -98,11 +100,11 @@ module Dashboard next end response = row.survey_item_response(survey_item:) - build_response(survey_item_response: response, likert_score:, row:, survey_item:) + build_response(survey_item_response: response, likert_score:, row:, survey_item:, student:) end.compact end - def build_response(survey_item_response:, likert_score:, row:, survey_item:) + def build_response(survey_item_response:, likert_score:, row:, survey_item:, student:) gender = genders[row.gender] grade = row.grade income = incomes[row.income.parameterize] @@ -120,7 +122,8 @@ module Dashboard recorded_date:, dashboard_income_id: income.id, dashboard_ell_id: ell.id, - dashboard_sped_id: sped.id } + dashboard_sped_id: sped.id, + dashboard_student_id: student.id } end def survey_items(headers:)