From ec3088fdec5fab9a8285dcf185312e3338258d0e Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 13 Jan 2012 15:56:31 -0600 Subject: [PATCH] ensure that pet state is saved before trying to save its assets --- app/models/item.rb | 11 +++++------ app/models/pet.rb | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index fc0d737b..497fc24e 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -284,9 +284,9 @@ class Item < ActiveRecord::Base swf_asset_ids << asset_id.to_i if asset_data end existing_swf_assets = SwfAsset.object_assets.find_all_by_remote_id swf_asset_ids - existing_swf_assets_by_id = {} + existing_swf_assets_by_remote_id = {} existing_swf_assets.each do |swf_asset| - existing_swf_assets_by_id[swf_asset.remote_id] = swf_asset + existing_swf_assets_by_remote_id[swf_asset.remote_id] = swf_asset end # With each asset in the registry, @@ -306,11 +306,11 @@ class Item < ActiveRecord::Base item.current_body_id = pet_type.body_id # Build and update the SWF - swf_asset_id = asset_data[:asset_id].to_i - swf_asset = existing_swf_assets_by_id[swf_asset_id] + swf_asset_remote_id = asset_data[:asset_id].to_i + swf_asset = existing_swf_assets_by_remote_id[swf_asset_remote_id] unless swf_asset swf_asset = SwfAsset.new - swf_asset.remote_id = swf_asset_id + swf_asset.remote_id = swf_asset_remote_id end swf_asset.origin_object_data = asset_data swf_asset.origin_pet_type = pet_type @@ -320,7 +320,6 @@ class Item < ActiveRecord::Base unless relationship relationship = ParentSwfAssetRelationship.new relationship.parent = item - relationship.swf_asset_id = swf_asset.id end relationship.swf_asset = swf_asset relationships_by_item_id[item_id] ||= [] diff --git a/app/models/pet.rb b/app/models/pet.rb index be151a9c..799479bf 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -66,14 +66,14 @@ class Pet < ActiveRecord::Base before_validation do pet_type.save! if @pet_state - @pet_state.handle_assets! @pet_state.save! + @pet_state.handle_assets! end if @items @items.each do |item| - item.handle_assets! item.save! + item.handle_assets! end end end