From 8430949a37cac1d0923c106116594d7696978bcf Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Sat, 11 Dec 2021 03:08:43 +0000 Subject: [PATCH] Fixed a couple slow queries --- app/controllers/api_controller.rb | 4 ++++ app/controllers/schools_controller.rb | 3 +-- app/controllers/tournaments_controller.rb | 4 +++- app/models/weight.rb | 2 +- app/views/api/tournament.jbuilder | 10 +++++----- app/views/tournaments/all_brackets.html.erb | 2 +- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 0865258..e652d6a 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -15,6 +15,10 @@ class ApiController < ApplicationController def tournament @tournament = Tournament.where(:id => params[:tournament]).includes(:schools,:weights,:mats,:matches,:user,:wrestlers).first + @schools = @tournament.schools.includes(:wrestlers) + @weights = @tournament.weights.includes(:wrestlers) + @matches = @tournament.matches.includes(:wrestlers,:schools) + @mats = @tournament.mats.includes(:matches) end def newTournament diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 0d25823..bcee0db 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -12,8 +12,7 @@ class SchoolsController < ApplicationController # GET /schools/1.json def show session.delete(:return_path) - @wrestlers = @school.wrestlers.includes(:deductedPoints,:matches,:weight) - @tournament = @school.tournament + @wrestlers = @school.wrestlers.includes(:deductedPoints,:matches,:weight,:school) end # GET /schools/new diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 64349bb..c499cc0 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -174,12 +174,14 @@ class TournamentsController < ApplicationController def all_brackets @schools = @tournament.schools @schools = @schools.sort_by{|s| s.page_score_string}.reverse! + @matches = @tournament.matches.includes(:wrestlers,:schools) + @weights = @tournament.weights.includes(:matches,:wrestlers) end def bracket if params[:weight] @weight = Weight.where(:id => params[:weight]).includes(:matches,:wrestlers).first - @matches = @weight.matches + @matches = @weight.matches.includes(:schools,:wrestlers) @wrestlers = @weight.wrestlers.includes(:school) if @tournament.tournament_type == "Pool to bracket" @pools = @weight.pool_rounds(@matches) diff --git a/app/models/weight.rb b/app/models/weight.rb index 2f170dc..e306e5f 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -10,7 +10,7 @@ class Weight < ActiveRecord::Base # passed via layouts/_tournament-navbar.html.erb # tournaments controller does a .split(',') on this string and creates an array via commas # tournament model runs the code via method create_pre_defined_weights - HS_WEIGHT_CLASSES = "106,113,120,126,132,138,144,150,157,165,175,190,215,285." + HS_WEIGHT_CLASSES = "106,113,120,126,132,138,144,150,157,165,175,190,215,285" HS_GIRLS_WEIGHT_CLASSES = "100,105,110,115,120,125,130,135,140,145,155,170,190,235" before_destroy do diff --git a/app/views/api/tournament.jbuilder b/app/views/api/tournament.jbuilder index c4aff85..bcd807e 100644 --- a/app/views/api/tournament.jbuilder +++ b/app/views/api/tournament.jbuilder @@ -3,13 +3,13 @@ json.cache! ["api_tournament", @tournament] do json.content(@tournament) json.(@tournament, :id, :name, :address, :director, :director_email, :tournament_type, :created_at, :updated_at, :user_id) - json.schools @tournament.schools do |school| + json.schools @schools do |school| json.id school.id json.name school.name json.score school.score end - json.weights @tournament.weights do |weight| + json.weights @weights do |weight| json.id weight.id json.max weight.max json.bracket_size weight.bracket_size @@ -26,7 +26,7 @@ json.cache! ["api_tournament", @tournament] do end end - json.mats @tournament.mats do |mat| + json.mats @mats do |mat| json.name mat.name json.unfinished_matches mat.unfinished_matches do |match| json.bout_number match.bout_number @@ -35,7 +35,7 @@ json.cache! ["api_tournament", @tournament] do end end - json.unassignedMatches @tournament.matches.select{|m| m.mat_id == nil}.sort_by{|m| m.bout_number}[0...9] do |match| + json.unassignedMatches @matches.select{|m| m.mat_id == nil}.sort_by{|m| m.bout_number}[0...9] do |match| json.bout_number match.bout_number json.w1_name match.w1_name json.w2_name match.w2_name @@ -43,7 +43,7 @@ json.cache! ["api_tournament", @tournament] do json.round match.round end - json.matches @tournament.matches do |match| + json.matches @matches do |match| json.bout_number match.bout_number json.w1_name match.w1_name json.w2_name match.w2_name diff --git a/app/views/tournaments/all_brackets.html.erb b/app/views/tournaments/all_brackets.html.erb index cf8b00f..4576b17 100644 --- a/app/views/tournaments/all_brackets.html.erb +++ b/app/views/tournaments/all_brackets.html.erb @@ -16,7 +16,7 @@ <% cache ["#{@tournament.id}_all_brackets", @tournament] do %>
<%= render :template => 'tournaments/team_scores' %> - <% @tournament.weights.sort_by{|w| w.max}.each do |weight| %> + <% @weights.sort_by{|w| w.max}.each do |weight| %> <% if @tournament.tournament_type == "Pool to bracket" %>