Skip to content

Instantly share code, notes, and snippets.

@malandrina
Last active December 18, 2015 23:29
Show Gist options
  • Save malandrina/5862099 to your computer and use it in GitHub Desktop.
Save malandrina/5862099 to your computer and use it in GitHub Desktop.
def filter_by_highest_ranked_user_coaches
coach_user_ids_string = coach_user_ids.to_s.gsub('[', '').gsub(']', '')
highest_ranked_coach_ids = ActiveRecord::Base.connection.execute(<<-TEXT
SELECT DISTINCT on (user_id) user_id, id
FROM coaches c
WHERE coachup_ranking_score =
(SELECT MAX(r.coachup_ranking_score)
FROM coaches r
WHERE c.user_id = r.user_id
AND c.user_id IN (#{coach_user_ids_string}))
TEXT
).values.map { |user_id, coach_id| coach_id }
@scope.where(coach_id: highest_ranked_coach_ids)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment