From e97aa0d6801ce52f727450ca28a77cdfdfa18822 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Fri, 13 Feb 2026 18:02:04 -0500 Subject: [PATCH] Fixed N+1 on up_matches and added html cache. --- app/models/mat.rb | 24 +++++- .../tournaments/_up_matches_mat_row.html.erb | 34 ++++++++ app/views/tournaments/up_matches.html.erb | 34 +------- test/controllers/up_matches_cache_test.rb | 84 +++++++++++++++++++ test/models/mat_test.rb | 15 ++++ 5 files changed, 155 insertions(+), 36 deletions(-) create mode 100644 app/views/tournaments/_up_matches_mat_row.html.erb create mode 100644 test/controllers/up_matches_cache_test.rb diff --git a/app/models/mat.rb b/app/models/mat.rb index 2733b28..9a60b16 100644 --- a/app/models/mat.rb +++ b/app/models/mat.rb @@ -8,6 +8,8 @@ class Mat < ApplicationRecord QUEUE_SLOTS = %w[queue1 queue2 queue3 queue4].freeze + after_save :clear_queue_matches_cache + def assign_next_match slot = first_empty_queue_slot return true unless slot @@ -86,8 +88,20 @@ class Mat < ApplicationRecord QUEUE_SLOTS.map { |slot| public_send(slot) } end + # used to prevent N+1 query on each mat def queue_matches - queue_match_ids.map { |match_id| match_id ? Match.find_by(id: match_id) : nil } + slot_ids = queue_match_ids + if @queue_matches.nil? || @queue_match_slot_ids != slot_ids + ids = slot_ids.compact + @queue_matches = if ids.empty? + [nil, nil, nil, nil] + else + matches_by_id = Match.where(id: ids).index_by(&:id) + slot_ids.map { |match_id| match_id ? matches_by_id[match_id] : nil } + end + @queue_match_slot_ids = slot_ids + end + @queue_matches end def queue1_match @@ -175,9 +189,13 @@ class Mat < ApplicationRecord private + def clear_queue_matches_cache + @queue_matches = nil + @queue_match_slot_ids = nil + end + def queue_match_at(position) - match_id = public_send("queue#{position}") - match_id ? Match.find_by(id: match_id) : nil + queue_matches[position - 1] end def first_empty_queue_slot diff --git a/app/views/tournaments/_up_matches_mat_row.html.erb b/app/views/tournaments/_up_matches_mat_row.html.erb new file mode 100644 index 0000000..f4095ed --- /dev/null +++ b/app/views/tournaments/_up_matches_mat_row.html.erb @@ -0,0 +1,34 @@ +<% queue1_match, queue2_match, queue3_match, queue4_match = mat.queue_matches %> +<% cache ["up_matches_mat_row", mat, mat.queue1, mat.queue2, mat.queue3, mat.queue4] do %> + + <%= mat.name %> + + <% if queue1_match %><%= queue1_match.bout_number %> (<%= queue1_match.bracket_position %>)
+ <%= queue1_match.weight_max %> lbs +
<%= queue1_match.w1_bracket_name %> vs.
+ <%= queue1_match.w2_bracket_name %> + <% end %> + + + <% if queue2_match %><%= queue2_match.bout_number %> (<%= queue2_match.bracket_position %>)
+ <%= queue2_match.weight_max %> lbs +
<%= queue2_match.w1_bracket_name %> vs.
+ <%= queue2_match.w2_bracket_name %> + <% end %> + + + <% if queue3_match %><%= queue3_match.bout_number %> (<%= queue3_match.bracket_position %>)
+ <%= queue3_match.weight_max %> lbs +
<%= queue3_match.w1_bracket_name %> vs.
+ <%= queue3_match.w2_bracket_name %> + <% end %> + + + <% if queue4_match %><%= queue4_match.bout_number %> (<%= queue4_match.bracket_position %>)
+ <%= queue4_match.weight_max %> lbs +
<%= queue4_match.w1_bracket_name %> vs.
+ <%= queue4_match.w2_bracket_name %> + <% end %> + + +<% end %> diff --git a/app/views/tournaments/up_matches.html.erb b/app/views/tournaments/up_matches.html.erb index eebbf4b..d2c6960 100644 --- a/app/views/tournaments/up_matches.html.erb +++ b/app/views/tournaments/up_matches.html.erb @@ -1,4 +1,3 @@ -<% cache ["#{@tournament.id}_up_matches", @tournament] do %>