我正在尝试向库存中的现有商品金额添加金额。
虽然它没有加起来,我做错了什么?
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
答案 0 :(得分:1)
item_for_inventories似乎是与playerInventory的关系,你在更新ItemForInventory后保存了playerInventory,你应该保存ItemForInventory或者可以更新它..像@playerinventory.item_for_inventories.find_by_item_id(theItem).update_attributes(amount: newAmount)