Elixir Enum.reduce返回一个模型而不是一个数字

Elixir Enum.reduce返回一个模型而不是一个数字,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,在我的模型中,我有: # .... def total_price(self) do Enum.reduce(self.child_items, fn(x, acc) -> x.price + acc end) end 它返回一个ChildItem,而不是表示总价的数字。这是为什么?如何修复?Enum.reduce有两个版本:和。带有2个参数的一个参数将集合中的第一个元素作为初始累加器—在您的示例中,第一个ChildItem 您要做的是提供初始成本

在我的模型中,我有:

  # ....
  def total_price(self) do
    Enum.reduce(self.child_items, fn(x, acc) ->
      x.price + acc
    end)
  end

它返回一个ChildItem,而不是表示总价的数字。这是为什么?如何修复?

Enum.reduce有两个版本:和。带有2个参数的一个参数将集合中的第一个元素作为初始累加器—在您的示例中,第一个
ChildItem

您要做的是提供初始成本
0
作为累加器:

  def total_price(self) do
    Enum.reduce(self.child_items, 0, fn(x, acc) ->
      x.price + acc
    end)
  end

是否要将
0
作为第二个参数传递<代码>枚举减少(self.child_项,0,fn(…)->…结束)