Skip to content

Instantly share code, notes, and snippets.

@nicolasiensen
Last active December 14, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasiensen/5031385 to your computer and use it in GitHub Desktop.
Save nicolasiensen/5031385 to your computer and use it in GitHub Desktop.
Ordering games by achievements completeness
# 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