1
0
Fork 0
forked from OpenNeo/impress

dodge weird arel bug by preloading basic pet types

This commit is contained in:
Emi Matchu 2010-11-17 00:12:14 -05:00
parent 1f1c219005
commit 6903752933

View file

@ -9,21 +9,20 @@ class PetType < ActiveRecord::Base
BasicHashes = YAML::load_file(Rails.root.join('config', 'basic_type_hashes.yml'))
StandardBodyIds = PetType.select(arel_table[:body_id]).
where(arel_table[:color_id].in(Color::BasicIds)).
group(arel_table[:species_id]).map(&:body_id)
scope :random_basic_per_species, lambda { |species_ids|
conditions = nil
species_ids.each do |species_id|
color_id = Color::Basic[rand(Color::Basic.size)].id
condition = arel_table[:species_id].eq(species_id).and(
arel_table[:color_id].eq(color_id)
)
conditions = conditions ? conditions.or(condition) : condition
StandardPetTypesBySpeciesId = PetType.where(arel_table[:color_id].in(Color::BasicIds)).group_by(&:species_id)
StandardBodyIds = []
StandardPetTypesBySpeciesId.each do |species_id, pet_types|
StandardBodyIds += pet_types.map(&:body_id)
end
def self.random_basic_per_species(species_ids)
random_pet_types = []
species_ids.each do |species_id|
pet_types = StandardPetTypesBySpeciesId[species_id]
random_pet_types << pet_types[rand(pet_types.size)] if pet_types
end
random_pet_types
end
where(conditions).order(:species_id)
}
def as_json(options={})
if options[:for] == 'wardrobe'