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')
With slight variation from earlier
Job.group(:user_id).order('sum_total_days DESC').sum(:total_days)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With