From 4d314417e21b3134abf45edcb13a59d9dc3a9041 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 26 Jan 2012 13:51:30 -0600 Subject: [PATCH] fix parent-swf-asset-relationship destruction bug After changing the database structure, we lost the feature where, once we discover new assets for an item for a given body ID, we disconnect previously connected assets. This commit reinstates that feature. --- app/models/item.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index 497fc24e..0bf0e293 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -178,20 +178,21 @@ class Item < ActiveRecord::Base new_swf_asset_ids = @parent_swf_asset_relationships_to_update.map(&:swf_asset_id) rels = ParentSwfAssetRelationship.arel_table swf_assets = SwfAsset.arel_table + # If a relationship used to bind an item and asset for this body type, # but doesn't in this sample, the two have been unbound. Delete the # relationship. ids_to_delete = self.parent_swf_asset_relationships. - select(:remote_id). + select(rels[:id]). joins(:swf_asset). where(rels[:swf_asset_id].not_in(new_swf_asset_ids)). where(swf_assets[:body_id].in([@current_body_id, 0])). - map(&:remote_id) + map(&:id) + unless ids_to_delete.empty? - self.parent_swf_asset_relationships. - where(rels[:swf_asset_id].in(ids_to_delete)). - delete_all + ParentSwfAssetRelationship.where(:id => ids_to_delete).delete_all end + @parent_swf_asset_relationships_to_update.each do |rel| rel.save! rel.swf_asset.save!