From be91c2c03311454ace6e6dbe180572b6742c2c64 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 9 Feb 2025 18:00:14 -0800 Subject: [PATCH] Optimize party loading by adding eager loading to `set_from_slug` - Refactored `set_from_slug` to use `includes` for eager loading associated models: - `user`, `job`, `raid` (with `group`) - `characters` (with `character` and `awakening`) - `weapons` (with `weapon`, `awakenings`, `weapon_key1`, `weapon_key2`, `weapon_key3`) - `summons` (with `summon`) - `guidebooks` (`guidebook1`, `guidebook2`, `guidebook3`) - `source_party`, `remixes`, `skills`, and `accessory` - This change improves query efficiency by reducing N+1 queries and ensures all relevant associations are preloaded. - Removed redundant favorite check as it was not necessary in this context. --- app/controllers/api/v1/parties_controller.rb | 37 ++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index 3ab0151..4221116 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -670,12 +670,37 @@ module Api # Loads party by shortcode for routes using :id # @return [void] def set_from_slug - @party = Party.where('shortcode = ?', params[:id]).first - if @party - @party.favorited = current_user && @party ? @party.is_favorited(current_user) : false - else - render_not_found_response('party') - end + @party = Party.includes( + :user, + :job, + { raid: :group }, + { characters: [:character, :awakening] }, + { + weapons: { + # Eager load the associated weapon and its awakenings. + weapon: [:awakenings], + # Eager load the grid weapon’s own awakening (if applicable). + awakening: {}, + # Eager load any weapon key associations. + weapon_key1: {}, + weapon_key2: {}, + weapon_key3: {} + } + }, + { summons: :summon }, + :guidebook1, + :guidebook2, + :guidebook3, + :source_party, + :remixes, + :skill0, + :skill1, + :skill2, + :skill3, + :accessory + ).find_by(shortcode: params[:id]) + + render_not_found_response('party') unless @party end # Loads party by ID for update/destroy actions