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