Last active
December 14, 2015 04:49
-
-
Save nicolasiensen/5031385 to your computer and use it in GitHub Desktop.
Ordering games by achievements completeness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# by @diogob | |
def self.by_completeness user | |
user.games.order(" | |
CASE WHEN (SELECT count(*) FROM achievements a WHERE a.game_id = games.id) = 0 THEN 0 ELSE | |
(SELECT count(DISTINCT a.id) | |
FROM | |
achievements_users au | |
JOIN achievements a ON a.id = au.achievement_id | |
WHERE a.game_id = games.id AND au.user_id = #{user.id})::numeric / | |
(SELECT count(*) | |
FROM achievements a | |
WHERE a.game_id = games.id) END DESC | |
") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment