From d0dffd6cffe9d4d7d5fcb4cf78060ace46e9f07c Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 25 Jan 2013 10:44:15 -0600 Subject: [PATCH] fix ClosetHanger.set_quantity to not call destroy on a new hanger; it confuses flex --- app/models/closet_hanger.rb | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/models/closet_hanger.rb b/app/models/closet_hanger.rb index 2de08f20..ad3dde76 100644 --- a/app/models/closet_hanger.rb +++ b/app/models/closet_hanger.rb @@ -69,23 +69,28 @@ class ClosetHanger < ActiveRecord::Base end hanger = self.where(conditions).first - unless hanger - hanger = self.new - hanger.user_id = conditions[:user_id] - hanger.item_id = conditions[:item_id] - # One of the following will be nil, and that's okay. If owned is nil, - # we'll cover for it before validation, as always. - hanger.owned = conditions[:owned] - hanger.list_id = conditions[:list_id] - end - unless quantity == 0 - Rails.logger.debug("Logging to #{hanger.id} quantity #{quantity}") + if quantity > 0 + # If quantity is non-zero, create/update the corresponding hanger. + + unless hanger + hanger = self.new + hanger.user_id = conditions[:user_id] + hanger.item_id = conditions[:item_id] + # One of the following will be nil, and that's okay. If owned is nil, + # we'll cover for it before validation, as always. + hanger.owned = conditions[:owned] + hanger.list_id = conditions[:list_id] + end + hanger.quantity = quantity hanger.save! - else - hanger.destroy if hanger + elsif hanger + # If quantity is zero and there's a hanger, destroy it. + hanger.destroy end + + # If quantity is zero and there's no hanger, good. Do nothing. end protected