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.
This commit is contained in:
Justin Edmund 2025-02-09 18:00:14 -08:00
parent 844d3ee6f8
commit be91c2c033

View file

@ -670,12 +670,37 @@ module Api
# Loads party by shortcode for routes using :id # Loads party by shortcode for routes using :id
# @return [void] # @return [void]
def set_from_slug def set_from_slug
@party = Party.where('shortcode = ?', params[:id]).first @party = Party.includes(
if @party :user,
@party.favorited = current_user && @party ? @party.is_favorited(current_user) : false :job,
else { raid: :group },
render_not_found_response('party') { characters: [:character, :awakening] },
end {
weapons: {
# Eager load the associated weapon and its awakenings.
weapon: [:awakenings],
# Eager load the grid weapons 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 end
# Loads party by ID for update/destroy actions # Loads party by ID for update/destroy actions