1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-06 22:44:14 +00:00

Fixed a number of N+1 issues on low traffic pages. I also added relevant html tests for these pages.

This commit is contained in:
2026-02-17 22:27:11 -05:00
parent d359be3ea1
commit 8670ce38c3
16 changed files with 330 additions and 103 deletions

View File

@@ -96,7 +96,9 @@ class Mat < ApplicationRecord
@queue_matches = if ids.empty?
[nil, nil, nil, nil]
else
matches_by_id = Match.where(id: ids).index_by(&:id)
matches_by_id = Match.where(id: ids)
.includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches })
.index_by(&:id)
slot_ids.map { |match_id| match_id ? matches_by_id[match_id] : nil }
end
@queue_match_slot_ids = slot_ids

View File

@@ -5,6 +5,8 @@ class Match < ApplicationRecord
belongs_to :weight, touch: true
belongs_to :mat, touch: true, optional: true
belongs_to :winner, class_name: 'Wrestler', foreign_key: 'winner_id', optional: true
belongs_to :wrestler1, class_name: 'Wrestler', foreign_key: 'w1', optional: true
belongs_to :wrestler2, class_name: 'Wrestler', foreign_key: 'w2', optional: true
has_many :wrestlers, :through => :weight
has_many :schools, :through => :wrestlers
validate :score_validation, :win_type_validation, :bracket_position_validation, :overtime_type_validation
@@ -178,14 +180,6 @@ class Match < ApplicationRecord
end
end
def wrestler1
wrestlers.select{|w| w.id == self.w1}.first
end
def wrestler2
wrestlers.select{|w| w.id == self.w2}.first
end
def w1_name
if self.w1 != nil
wrestler1.name
@@ -203,7 +197,7 @@ class Match < ApplicationRecord
end
def w1_bracket_name
first_round = self.weight.matches.sort_by{|m| m.round}.first.round
first_round = first_round_for_weight
return_string = ""
return_string_ending = ""
if self.w1 and self.winner_id == self.w1
@@ -223,7 +217,7 @@ class Match < ApplicationRecord
end
def w2_bracket_name
first_round = self.weight.matches.sort_by{|m| m.round}.first.round
first_round = first_round_for_weight
return_string = ""
return_string_ending = ""
if self.w2 and self.winner_id == self.w2
@@ -289,6 +283,17 @@ class Match < ApplicationRecord
self.weight.max
end
def first_round_for_weight
return @first_round_for_weight if defined?(@first_round_for_weight)
@first_round_for_weight =
if association(:weight).loaded? && self.weight&.association(:matches)&.loaded?
self.weight.matches.map(&:round).compact.min
else
Match.where(weight_id: self.weight_id).minimum(:round)
end
end
def replace_loser_name_with_wrestler(w,loser_name)
if self.loser1_name == loser_name
self.w1 = w.id

View File

@@ -97,18 +97,11 @@ class Tournament < ApplicationRecord
end
def pointAdjustments
point_adjustments = []
self.schools.each do |s|
s.deductedPoints.each do |d|
point_adjustments << d
end
end
self.wrestlers.each do |w|
w.deductedPoints.each do |d|
point_adjustments << d
end
end
point_adjustments
school_scope = Teampointadjust.where(school_id: schools.select(:id))
wrestler_scope = Teampointadjust.where(wrestler_id: wrestlers.select(:id))
Teampointadjust.includes(:school, :wrestler)
.merge(school_scope.or(wrestler_scope))
end
def remove_school_delegations

View File

@@ -53,19 +53,16 @@ class User < ApplicationRecord
end
def delegated_tournaments
tournaments_delegated = []
delegated_tournament_permissions.each do |t|
tournaments_delegated << t.tournament
end
tournaments_delegated
Tournament.joins(:delegates)
.where(tournament_delegates: { user_id: id })
.distinct
end
def delegated_schools
schools_delegated = []
delegated_school_permissions.each do |t|
schools_delegated << t.school
end
schools_delegated
School.joins(:delegates)
.where(school_delegates: { user_id: id })
.includes(:tournament)
.distinct
end
def self.search(search)