的未定义方法`assign_attributes’:Array

我的应用程序设置为当product.sold属性为1表示商品已售出且它不会显示在商店视图中。 我试图得到它,所以当客户签出product.sold属性时,购买该项目时更新。

以下是我的控制器中应该将product.sold属性从nil更新为1的代码行:

class ChargesController < ApplicationController def create @order.order_items.map{ |o| o.product.id }.assign_attributes(sold: 1) end 

这是我的协会:

 OrderItem belongs_to :product OrderItem belongs_to :order Product has_many :order_items Order has_many :order_items 

这是我在尝试购买ID为13的产品时遇到的错误

 NoMethodError in ChargesController#create undefined method `assign_attributes' for [13]:Array Extracted source (around line #12): #OrderMailer.order_email(@order).deliver_now @order.update_attribute(:order_status_id, 2) @order.order_items.map{ |o| o.product.id }.assign_attributes(sold: 1) @final_amount = @amount 

我也尝试了update_attributes并更新了assign_attributes。 不知道还有什么可以尝试。 如果需要更多信息,请与我们联系。

您试图在产品ID数组上调用assign_attributes ,而不是模型。 试试这个:

 def create product_ids = @order.order_items.map{ |o| o.product.id } Product.where(id: product_ids).update_all(sold: 1) end