diff --git a/app/models/pet.rb b/app/models/pet.rb index 27e6e655..c309ce90 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -63,6 +63,7 @@ class Pet < ActiveRecord::Base before_validation do pet_type.save! + @pet_state.save! items.each do |item| item.handle_assets! item.save! diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index 17afa3ff..e6d88c6a 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -12,6 +12,9 @@ class PetState < ActiveRecord::Base alias_method :swf_asset_ids_from_association, :swf_asset_ids + scope :emotion_order, joins(:parent_swf_asset_relationships). + group("pet_states.id").order("COUNT(parents_swf_assets.swf_asset_id) DESC, SUM(parents_swf_assets.swf_asset_id) ASC") + def reassign_children_to!(main_pet_state) self.contributions.each do |contribution| contribution.contributed = main_pet_state diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index bd46b372..0767fddc 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -34,7 +34,7 @@ class PetType < ActiveRecord::Base def as_json(options={}) if options[:for] == 'wardrobe' - {:id => id, :body_id => body_id, :pet_state_ids => pet_state_ids} + {:id => id, :body_id => body_id, :pet_state_ids => pet_states.select([:id]).emotion_order.map(&:id)} else {:image_hash => image_hash} end diff --git a/app/models/user.rb b/app/models/user.rb index b804e5c9..9abe043b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,6 +23,7 @@ class User < ActiveRecord::Base self.points += new_points Pet.transaction do pet.save! + new_contributions.each(&:save!) save! end new_points