Skip to content

Instantly share code, notes, and snippets.

@nicolasiensen nicolasiensen/game.rb
Last active Dec 14, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.