我正在运行此查询
Device.joins(:reviews).group(:brand).average(:average_rating)
...并获得此输出
{#<Brand id: 1, name: "Phonak", created_at: "2013-08-26 22:25:07", updated_at: "2013-08-26 22:25:07">=>#<BigDecimal:6b4dc90,'0.788E2',18(45)>, #<Brand id: 2, name: "Oticon", created_at: "2013-08-29 21:03:06", updated_at: "2013-08-29 21:03:06">=>#<BigDecimal:6b4e320,'0.8070000712 076823E2',27(45)>, #<Brand id: 5, name: "Siemens", created_at: "2013-09-07 21:34:18", updated_at: "2013-09-07 21:34:18">=>#<BigDecimal:6b4eb68,'0.8111111111 111111E2',27(45)>, #<Brand id: 7, name: "Resound", created_at: "2013-09-13 16:10:34", updated_at: "2013-09-13 16:10:34">=>#<BigDecimal:6b4f090,'0.7342857142 857143E2',27(45)>}
但是,我希望将结果按品牌名称分组,而不是实际的品牌对象。这可能吗。
我试过了:
Device.joins(:reviews).group('brand.name').average(:average_rating)
....但这不起作用
答案 0 :(得分:3)
我会尝试
Device.joins(:reviews).includes(:brand).group("brands.name").average(:average_rating)