preload crews and favorites to fix remaining N+1s
This commit is contained in:
parent
6f3f0d92ff
commit
56280eb0ff
3 changed files with 21 additions and 5 deletions
|
|
@ -28,7 +28,12 @@ module Api
|
|||
|
||||
# Metadata associations
|
||||
field :favorited do |party, options|
|
||||
party.favorited?(options[:current_user])
|
||||
# Use preloaded favorite_party_ids if available, otherwise fall back to query
|
||||
if options[:favorite_party_ids]
|
||||
options[:favorite_party_ids].include?(party.id)
|
||||
else
|
||||
party.favorited?(options[:current_user])
|
||||
end
|
||||
end
|
||||
|
||||
# For collection views
|
||||
|
|
|
|||
|
|
@ -11,8 +11,11 @@ module Api
|
|||
element: user.element
|
||||
}
|
||||
end
|
||||
field :gamertag, if: ->(_, user, _) { user.show_gamertag && user.crew&.gamertag.present? } do |user|
|
||||
user.crew.gamertag
|
||||
# Use preloaded active_crew_membership to avoid N+1
|
||||
field :gamertag, if: ->(_, user, _) {
|
||||
user.show_gamertag && user.active_crew_membership&.crew&.gamertag.present?
|
||||
} do |user|
|
||||
user.active_crew_membership.crew.gamertag
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -188,12 +188,16 @@ module Api
|
|||
query = build_filtered_query(build_common_base_query)
|
||||
@parties = query.paginate(page: params[:page], per_page: page_size)
|
||||
|
||||
# Preload current user's favorite party IDs to avoid N+1
|
||||
favorite_party_ids = current_user ? current_user.favorites.pluck(:party_id).to_set : Set.new
|
||||
|
||||
render json: Api::V1::PartyBlueprint.render(
|
||||
@parties,
|
||||
view: :preview,
|
||||
root: :results,
|
||||
meta: pagination_meta(@parties),
|
||||
current_user: current_user
|
||||
current_user: current_user,
|
||||
favorite_party_ids: favorite_party_ids
|
||||
)
|
||||
end
|
||||
|
||||
|
|
@ -208,12 +212,16 @@ module Api
|
|||
query = build_filtered_query(base_query)
|
||||
@parties = query.paginate(page: params[:page], per_page: page_size)
|
||||
|
||||
# All parties in this list are favorites, but preload for consistency
|
||||
favorite_party_ids = current_user.favorites.pluck(:party_id).to_set
|
||||
|
||||
render json: Api::V1::PartyBlueprint.render(
|
||||
@parties,
|
||||
view: :preview,
|
||||
root: :results,
|
||||
meta: pagination_meta(@parties),
|
||||
current_user: current_user
|
||||
current_user: current_user,
|
||||
favorite_party_ids: favorite_party_ids
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue