AME
AME

Reputation: 2302

How to sort that query?

Probably it's very easy but it seems like I can't figure it out...

How do I sort that query ACS:

@most_valuable_clients = current_user.clients.includes(:treatments).group(:vorname, :nachname, "clients.id").sum(:preis)

This query produces a hash - I can call query.to_a.sort_by() on it but it slows down everything.

right now it's like:

1. john 21 dollar
2. mary 25 dollar
3. bob 30 dollar

It should be:

1. bob 30 dollar
2. mary 25 dollar
3. john 21 dollar

preis is basically integers and I want to sort them. I know that I have to use order but all I got are errors.

How?

Upvotes: 0

Views: 38

Answers (1)

mohameddiaa27
mohameddiaa27

Reputation: 3597

You need to order before SUM

@most_valuable_clients = current_user.clients.includes(:treatments).group(:vorname, :nachname, "clients.id").order('SUM(preis) DESC').sum(:preis)

Upvotes: 2

Related Questions