2016-12-23 63 views
3

我的應用程序設置爲當product.sold屬性爲1時表示商品已售出,並且不會在商店視圖中顯示。我試圖在客戶簽出product.sold屬性時更新購買該商品。未定義的方法`assign_attributes'爲[13]:Array

這裏的代碼在我的控制器應該從零到1被更新product.sold屬性行:

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 

我也試過upd ate_attributes和更新來代替assign_attributes。不知道還有什麼要嘗試。讓我知道是否需要更多信息。

+0

那麼你想要爲每個項目的順序設置一個屬性? –

回答

4

您試圖在產品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 
相關問題