From ac41b3f3c9036933418cd1a9069060293ad6a069 Mon Sep 17 00:00:00 2001 From: yamat47 Date: Thu, 15 Aug 2024 23:41:17 +0900 Subject: [PATCH] Fix N+1. --- app/.rubocop.yml | 1 + app/app/controllers/admin/game_schedules_controller.rb | 1 + app/app/controllers/admin/media_page_settings_controller.rb | 2 +- app/app/controllers/media/game_schedules_controller.rb | 2 +- app/app/controllers/media/homes_controller.rb | 2 ++ app/app/views/admin/game_schedules/_form.html.haml | 4 ++-- app/config/environments/development.rb | 3 +++ 7 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/.rubocop.yml b/app/.rubocop.yml index 1ac2362..f157647 100644 --- a/app/.rubocop.yml +++ b/app/.rubocop.yml @@ -21,6 +21,7 @@ Metrics/AbcSize: Metrics/BlockLength: Exclude: - 'spec/**/*_spec.rb' + - 'config/environments/development.rb' Metrics/MethodLength: Exclude: diff --git a/app/app/controllers/admin/game_schedules_controller.rb b/app/app/controllers/admin/game_schedules_controller.rb index 0bdd2bb..10e877e 100644 --- a/app/app/controllers/admin/game_schedules_controller.rb +++ b/app/app/controllers/admin/game_schedules_controller.rb @@ -7,6 +7,7 @@ class GameSchedulesController < ApplicationController def index @game_schedules = GameSchedule.start_at_ordered .search_by_keyword(index_params[:search]) + .preload(:season, :tournament, :home_team, :visitor_team, :game_field) .page(index_params[:page]) .per(INDEX_PER_PAGE) end diff --git a/app/app/controllers/admin/media_page_settings_controller.rb b/app/app/controllers/admin/media_page_settings_controller.rb index f1a43fa..e66dac9 100644 --- a/app/app/controllers/admin/media_page_settings_controller.rb +++ b/app/app/controllers/admin/media_page_settings_controller.rb @@ -3,7 +3,7 @@ module Admin class MediaPageSettingsController < ApplicationController def show - @media_hero_images = MediaHeroImage.sort_order_ordered + @media_hero_images = MediaHeroImage.preload(image_attachment: :blob).sort_order_ordered end end end diff --git a/app/app/controllers/media/game_schedules_controller.rb b/app/app/controllers/media/game_schedules_controller.rb index b635457..9f6d451 100644 --- a/app/app/controllers/media/game_schedules_controller.rb +++ b/app/app/controllers/media/game_schedules_controller.rb @@ -12,7 +12,7 @@ def index game_schedules = GameSchedule.start_at_ordered .game_field_ordered .where(season: @season) - .preload(:home_team, :visitor_team, :game_field, :tournament) + .preload(:home_team, :visitor_team, :game_field, :tournament, :game_result) # group by date. @game_schedule_groups = game_schedules.group_by { |game_schedule| game_schedule.start_at.to_date } diff --git a/app/app/controllers/media/homes_controller.rb b/app/app/controllers/media/homes_controller.rb index 3202e7c..aa9a6d7 100644 --- a/app/app/controllers/media/homes_controller.rb +++ b/app/app/controllers/media/homes_controller.rb @@ -6,12 +6,14 @@ class HomesController < ApplicationController def show @media_hero_images = MediaHeroImage.sort_order_ordered + .preload(image_attachment: :blob) @notices = Notice.only_published .published_at_ordered .limit(NOTICES_LIMIT) @game_schedules = GameSchedule.next_scheduled_games + .preload(:game_field, :tournament, :home_team, :visitor_team, :game_result) end end end diff --git a/app/app/views/admin/game_schedules/_form.html.haml b/app/app/views/admin/game_schedules/_form.html.haml index 8f39401..3b03c8d 100644 --- a/app/app/views/admin/game_schedules/_form.html.haml +++ b/app/app/views/admin/game_schedules/_form.html.haml @@ -31,7 +31,7 @@ = GameSchedule.human_attribute_name(:home_team) .admin-field__value = form.select_with_image(:home_team_id, - Team.all, + Team.preload({ logo_attachment: :blob }), value_method: :id, text_method: :name, image_method: :logo_url, @@ -43,7 +43,7 @@ = GameSchedule.human_attribute_name(:visitor_team) .admin-field__value = form.select_with_image(:visitor_team_id, - Team.all, + Team.preload({ logo_attachment: :blob }), value_method: :id, text_method: :name, image_method: :logo_url, diff --git a/app/config/environments/development.rb b/app/config/environments/development.rb index a9705c3..2ca5b50 100644 --- a/app/config/environments/development.rb +++ b/app/config/environments/development.rb @@ -76,5 +76,8 @@ Bullet.rails_logger = true Bullet.add_footer = true Bullet.skip_html_injection = false + + Bullet.add_safelist type: :unused_eager_loading, class_name: 'ActiveStorage::Attachment', + association: :blob end end