Ruby on rails 选择1作为一个-Ruby查询在staging和prod中回滚
在我的rails应用程序上成功付款后,我正在尝试更新products表的数量。 我就是这样做的Ruby on rails 选择1作为一个-Ruby查询在staging和prod中回滚,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,在我的rails应用程序上成功付款后,我正在尝试更新products表的数量。 我就是这样做的 @orders1 = Order.where("shoppingcart_id" => @cart.id) @orders1.each do |order| @oproduct = Product.where("id" => order.product.id).first if(@oproduct.quantity >=1 ) @oproduct.quantity = @
@orders1 = Order.where("shoppingcart_id" => @cart.id)
@orders1.each do |order|
@oproduct = Product.where("id" => order.product.id).first
if(@oproduct.quantity >=1 )
@oproduct.quantity = @oproduct.quantity - 1;
@oproduct.save
end
end
当我查看查询日志时,我看到Ruby将select 1作为一个查询运行-
Product Exists (1.6ms) SELECT 1 AS one FROM "products" WHERE
"products"."name" = ? AND "products"."id" != ? LIMIT ?
在我的本地(SQLLite)上,它运行良好,没有任何回滚,并且我的产品数量得到了正确更新。
但当我在登台上运行相同的程序时,它失败了。事务回滚。
在另一台本地计算机上,它也会发生故障。我无法在我的本地计算机上重现这一点,也不知道为什么会发生这种情况。其他的更新工作得非常好,比如订单更新。只有产品数量更新失败
该产品是否必须存在?不确定我是否缺少任何登台设置,或者代码是否有问题。试试这个
@product=product.find(order.product.id)
如果@product.quantity>0
@product.update\u属性!(数量:@product.quantity-1)
结束
能否显示product.rb中存在的验证?您可以使用@opproduct.save!要知道是否有任何ActiveRecord错误。@NimishGupta这里是validates:name,:description,:price,:picture,:quantity,presence:true验证:seo\u title,presence:true,length:{maximum:75},on::create validates:seo\u description,presence:true,length:{maximum:120},on::create Attown\u to:店铺Attown\u to:类别有\u多个:订单验证\u唯一性\u of:名称