dodge weird arel bug by preloading basic pet types
This commit is contained in:
parent
1f1c219005
commit
6903752933
1 changed files with 11 additions and 12 deletions
|
@ -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)
|
||||||
|
end
|
||||||
|
|
||||||
scope :random_basic_per_species, lambda { |species_ids|
|
def self.random_basic_per_species(species_ids)
|
||||||
conditions = nil
|
random_pet_types = []
|
||||||
species_ids.each do |species_id|
|
species_ids.each do |species_id|
|
||||||
color_id = Color::Basic[rand(Color::Basic.size)].id
|
pet_types = StandardPetTypesBySpeciesId[species_id]
|
||||||
condition = arel_table[:species_id].eq(species_id).and(
|
random_pet_types << pet_types[rand(pet_types.size)] if pet_types
|
||||||
arel_table[:color_id].eq(color_id)
|
|
||||||
)
|
|
||||||
conditions = conditions ? conditions.or(condition) : condition
|
|
||||||
end
|
end
|
||||||
where(conditions).order(:species_id)
|
random_pet_types
|
||||||
}
|
end
|
||||||
|
|
||||||
def as_json(options={})
|
def as_json(options={})
|
||||||
if options[:for] == 'wardrobe'
|
if options[:for] == 'wardrobe'
|
||||||
|
|
Loading…
Reference in a new issue