I’m trying to select some data from another query.
The SQL will look like this:
SELECT user_id, rank FROM
(SELECT user_id, MAX(created_at) as latest_solution, COUNT(*) AS solved,
rank() OVER (ORDER by COUNT(*) desc) AS rank FROM submissions group by user_id)
as leaderboard WHERE leaderboard.user_id = xx
but I’m having trouble trying to translate this into activerecord language
I think I’m doing ok with the following subquery:
Submission.select('user_id, MAX(created_at) as latest_solution, COUNT(*) as solved, rank() OVER (ORDER BY count(*) desc) as rank').group('user_id')
but I don’t know how to use this as a “table”.
I go straight to
find_by_sqlfor anything non-trivial:Wrap that in a class method on
Submissionand you should be okay.