添加现有金额不起作用的金额

时间:2014-01-26 18:57:33

标签: ruby-on-rails ruby-on-rails-4

我正在尝试向库存中的现有商品金额添加金额。

虽然它没有加起来,我做错了什么?

  def add_item_to_inventory (theItem, theAmount)
    @playerinventory = Playerinventory.find(params[:id])
    @item = Item.find(theItem)

    if !@item.nil?
      if !@playerinventory.item_for_inventories.find_by_item_id(theItem)
        @ifi = ItemForInventory.create(:item => @item, :amount => theAmount)
        @playerinventory.item_for_inventories << @ifi

      else
        oldAmount = @playerinventory.item_for_inventories.find_by_item_id(theItem).amount
        newAmount = oldAmount.to_i + theAmount.to_i

        @playerinventory.item_for_inventories.find_by_item_id(theItem).amount = newAmount
      end

      if @playerinventory.save 
        return true
      else
        return false
      end

    end

    false
  end

if部分就像一个魅力。 else没有

注意,theItem是item_id。显然,在我做@playerinventory.save

之后

1 个答案:

答案 0 :(得分:1)

item_for_inventories似乎是与playerInventory的关系,你在更新ItemForInventory后保存了playerInventory,你应该保存ItemForInventory或者可以更新它..像@playerinventory.item_for_inventories.find_by_item_id(theItem).update_attributes(amount: newAmount)