0

ショッピング カート機能を備えた Rails アプリがあり、完了したすべての注文で販売された個々の製品の合計数量を取得するメソッドを定義したいと考えています。やってみた

def total_product_sold (product)    
  product.line_items.to_a.sum { |item| item.quantity } 
end

ただし、これは、ショッピング カートにのみ配置され、注文されていないものを含むすべての項目の合計を返します。実際に注文した商品の合計のみが必要です。

私も.where次のようにメソッドを使用してみました

def total_product_sold (product)     
  product.line_items.to_a.sum   { |line_items.where("order > 0", params[:orders])|       line_items.quantity }
end
4

1 に答える 1

0

class LineItem < ActiveRecord::Base
  belongs_to :product
  scope :ordered, -> {
    where("ordered > 0")
  }
end

class Product < ActiveRecord::Base has_many :line_items def sold self.line_items.ordered end end

于 2013-07-29T15:19:42.980 に答える