Rails ActiveRecord:分组查询中缺少列 [英] Rails ActiveRecord: Missing column in grouping query

查看:33
本文介绍了Rails ActiveRecord:分组查询中缺少列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Key.select('products.name as product, product_groups.name as product_group, AVG(keys.cost) as cost')
.group('products.id, product_groups.id')
.left_joins(:product,:product_group)

结果:

=> #<ActiveRecord::Relation [#<Key id: nil, cost: 0.6e1>, #<Key id: nil, cost: 0.4e1>]>

预期返回 3 字段,但返回值:2 字段.

Expected return 3 field, but returnig value: 2 field.

I found the solution. The detail areas in the console did not appear as HASH.

推荐答案

根据我的理解,分组语句只会返回聚合列和用于对数据集进行分组的列.在您的情况下,您没有使用选择列表中的分组列,而是使用了其他一些字段.因此,您没有收到其他两列.

In my understanding the grouping statement would only return Aggregated columns and columns that are used to group the data set. In your case you have not used the grouping columns in the select list but, some other fields. As a result you don't receive the other two columns.

这篇关于Rails ActiveRecord:分组查询中缺少列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆