forked from OpenNeo/impress
Drastically reduce queries for item page preview
Oh right okay, I made a sloppy perf hack long ago, and now let's actually clean it up!
This commit is contained in:
parent
176ab20fd1
commit
edcb21558a
2 changed files with 13 additions and 7 deletions
|
@ -555,10 +555,10 @@ class Item < ApplicationRecord
|
|||
# Otherwise, create an appearance for each real (nonzero) body ID. We don't
|
||||
# generally expect body_id = 0 and body_id != 0 to mix, but if they do,
|
||||
# uhh, let's merge the body_id = 0 ones in?
|
||||
species_by_body_id = Species.with_body_ids(swf_assets_by_body_id.keys)
|
||||
swf_assets_by_body_id.map do |body_id, body_specific_assets|
|
||||
swf_assets_for_body = body_specific_assets + swf_assets_for_all_bodies
|
||||
species = Species.with_body_id(body_id).first!
|
||||
body = Appearance::Body.new(body_id, species)
|
||||
body = Appearance::Body.new(body_id, species_by_body_id[body_id])
|
||||
Appearance.new(self, body, swf_assets_for_body)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,11 +4,6 @@ class Species < ApplicationRecord
|
|||
|
||||
scope :alphabetical, -> { order(:name) }
|
||||
|
||||
scope :with_body_id, -> body_id {
|
||||
pt = PetType.arel_table
|
||||
joins(:pet_types).where(pt[:body_id].eq(body_id)).limit(1)
|
||||
}
|
||||
|
||||
def as_json(options={})
|
||||
super({only: [:id, :name], methods: [:human_name]}.merge(options))
|
||||
end
|
||||
|
@ -20,4 +15,15 @@ class Species < ApplicationRecord
|
|||
I18n.translate('species.default_human_name')
|
||||
end
|
||||
end
|
||||
|
||||
# Given a list of body IDs, return a hash from body ID to Species.
|
||||
# (We assume that each body ID belongs to just one species; if not, which
|
||||
# species we return for that body ID is undefined.)
|
||||
def self.with_body_ids(body_ids)
|
||||
species_ids_by_body_id = PetType.where(body_id: body_ids).distinct.
|
||||
pluck(:body_id, :species_id).to_h
|
||||
species_by_id = Species.where(id: species_ids_by_body_id.values).
|
||||
to_h { |s| [s.id, s] }
|
||||
species_ids_by_body_id.transform_values { |id| species_by_id[id] }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue