Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用MoneyRails时如何在Mongoid上获得平均值_Ruby On Rails_Mongoid_Currency_Money Rails - Fatal编程技术网

Ruby on rails 使用MoneyRails时如何在Mongoid上获得平均值

Ruby on rails 使用MoneyRails时如何在Mongoid上获得平均值,ruby-on-rails,mongoid,currency,money-rails,Ruby On Rails,Mongoid,Currency,Money Rails,我使用Money Rails作为字段,如下所示: class Product ... field :price_GBP, type: Money ... end 我试图得到所有price\u GBP文档的平均值。如果price\u GBP字段是浮点或整数,我会这样: avg = Product.avg(:price_GBP) 这将为我提供所有产品的平均价格。我试过: avg = Product.avg(":price_GBP.cents") #or avg = Product.a

我使用Money Rails作为字段,如下所示:

class Product
  ...
  field :price_GBP, type: Money
  ...
end
我试图得到所有
price\u GBP
文档的平均值。如果
price\u GBP
字段是浮点或整数,我会这样:

avg = Product.avg(:price_GBP)
这将为我提供所有产品的平均价格。我试过:

avg = Product.avg(":price_GBP.cents") #or
avg = Product.avg("price_GBP.fractional")
但两者都不起作用。有什么想法吗

编辑:

以下是
price\u GBP
属性:

price_GBP: {
    cents: 220,
    currency_iso: "GBP"
},

你看过
是如何存储在数据库中的吗?文档中有什么说明吗?你看过没有Mongoid的原始
price\u GBP
值了吗?钱是一个散列(在上面的编辑中),这就是为什么我认为
Product.avg(“:price\u GBP.cents”)
会奏效的原因。Money文档没有说什么,我正在Mongoid文档中寻找如何在不使用聚合框架的情况下对嵌套对象进行平均。在控制台中尝试一个简单的
Model.avg(some_field)
,然后查看您的Moped日志,您应该会看到幕后的聚合。您是否尝试过
Product.avg('price_GBP.cents')
(即字符串中没有冒号)?如果这不起作用,那么我猜你自己就要处理所有聚合管道的丑陋问题了。
Product.avg(“price\u GBP.fractal”)
,在你的嵌入式文档中没有
fractal
。尝试在mongodb中进行简单的计算。不,如果你的代码正常,我很高兴:)你看过
是如何存储在数据库中的吗?文档中有什么说明吗?你看过没有Mongoid的原始
price\u GBP
值了吗?钱是一个散列(在上面的编辑中),这就是为什么我认为
Product.avg(“:price\u GBP.cents”)
会奏效的原因。Money文档没有说什么,我正在Mongoid文档中寻找如何在不使用聚合框架的情况下对嵌套对象进行平均。在控制台中尝试一个简单的
Model.avg(some_field)
,然后查看您的Moped日志,您应该会看到幕后的聚合。您是否尝试过
Product.avg('price_GBP.cents')
(即字符串中没有冒号)?如果这不起作用,那么我猜你自己就要处理所有聚合管道的丑陋问题了。
Product.avg(“price\u GBP.fractal”)
,在你的嵌入式文档中没有
fractal
。尝试在mongodb中进行简单计算。不,如果您的代码正常,我很高兴:)