轨道,活动记录总和然后排序 [英] rails, activerecord sum then order

查看:72
本文介绍了轨道,活动记录总和然后排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Job模型,它属于Users,而User has_many个工作。我想创建一个AR查询,计算每个用户的工作日总数,然后按降序排列。



到目前为止,我已经知道了,但给我一个错误:(列 Job.id必须出现在GROUP BY子句中或在聚合函数中使用)

  @work_days = Job.group(:user).order('SUM(total_days)')

我似乎无法使用.order方法工作-我缺少什么吗?

解决方案

您可以编写查询:-

  Job.group(:user_id).select('SUM(total_days)as tot')。order('tot desc')


i have a Job model that belongs_to User, and User has_many jobs. I want to create an AR query that calculates the total number of work days per user, then orders in Descending order.

I have this so far, but is giving me an error: (column "Job.id" must appear in the GROUP BY clause or be used in an aggregate function)

@work_days = Job.group(:user).order('SUM(total_days)')

I can't seem to get the .order method to work - is there something I am missing? Thanks in advance!

解决方案

You could write your query :-

Job.group(:user_id).select('SUM(total_days) as tot').order('tot desc')

这篇关于轨道,活动记录总和然后排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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