Last active
December 18, 2015 23:29
-
-
Save malandrina/5862099 to your computer and use it in GitHub Desktop.
This file contains 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
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