I’m a rails newby, so I’m trying to wrap my head around how to tackle
this, since my first few attempts failed.
Let’s say your database looks like this:
id, name, created_at
id, user_id, value, created_at
Users complete different tasks and are awarded varying levels of points,
which are recorded in the Points table. What if you wanted to display a
“high score list”, showing the users in order by who had the most
points? It might look like:
Username A - 20 points
Username B - 14 points
Username C - 13 points
and so on…
You’d need to sum the values for each user_id, then display them in
order. And, based on the user_ids, you’d need to pull in and display
the users’ names from the Users table.
I’ve been attempting to do this using various incarnations of
Point.sum(:values, :group => ‘user_id’), but without luck. Using that
approach, I can display the point totals for each user_id, but the thing
breaks down when I try to bring in the names from the Users table.
Any help would be hugely appreciated!