Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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 Rails客户端订单历史记录在一个查询中_Ruby On Rails_Postgresql_Associations_Aggregate Functions - Fatal编程技术网

Ruby on rails Rails客户端订单历史记录在一个查询中

Ruby on rails Rails客户端订单历史记录在一个查询中,ruby-on-rails,postgresql,associations,aggregate-functions,Ruby On Rails,Postgresql,Associations,Aggregate Functions,我正在尝试生成一个客户配置文件页面。该页面包括客户订单历史记录、最新订单及其历史总计。我一直在运行sum、count和average on Customer.where_已完成,但这涉及到该数据库三次。我觉得一定有更好的办法 @customers_total_spent = @customer.orders.where_completed.sum(:sale) @customers_average_per_order = @customer.orders.where_completed.aver

我正在尝试生成一个客户配置文件页面。该页面包括客户订单历史记录、最新订单及其历史总计。我一直在运行sum、count和average on Customer.where_已完成,但这涉及到该数据库三次。我觉得一定有更好的办法

@customers_total_spent = @customer.orders.where_completed.sum(:sale)
@customers_average_per_order = @customer.orders.where_completed.average(:sale)
@customers_total_orders = @customer.orders.where_completed.count(:sale)
客户
class客户
命令
类顺序{where.not(completed_at:nil)}
结束
您应该试试这个

orders_completed = @customer.orders.where_completed.pluck("sum(sale), avg(sale), count(sale)").flatten
@customers_total_spent = orders_completed[0]
@customers_average_per_order = orders_completed[1]
@customers_total_orders = orders_completed[2]
你应该试试这个

orders_completed = @customer.orders.where_completed.pluck("sum(sale), avg(sale), count(sale)").flatten
@customers_total_spent = orders_completed[0]
@customers_average_per_order = orders_completed[1]
@customers_total_orders = orders_completed[2]
orders_completed = @customer.orders.where_completed.pluck("sum(sale), avg(sale), count(sale)").flatten
@customers_total_spent = orders_completed[0]
@customers_average_per_order = orders_completed[1]
@customers_total_orders = orders_completed[2]
@qry = @customer.orders.where_completed.select("sum(sale) as sum, avg(sale) as avg, count(sale) as count").first
@customers_total_spent = @qry.sum
@customers_average_per_order = @qry.avg
@customers_total_orders = @qry.count