I have two tables:
USERS
id name
------------
1. bill
2. dave
3. kate
4. sara
and
GAMES
score user_id
------------------
10 1
0 1
0 2
10 3
0 2
10 3
0 3
0 4
This query:
SELECT users.name, SUM(games.score) AS total FROM users JOIN games
ON users.id = games.user_id ORDER BY total LIMIT 2;
produces output like this:
name total
-------------
kate 20
bill 10
What is the correct Rails convention here for ActiveRecord to perform an ORDER BY with SUM and JOIN?