Created
October 30, 2011 21:10
-
-
Save robertomiranda/1326448 to your computer and use it in GitHub Desktop.
Finding recently next matches's players by category
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
#"SELECT "matches".* FROM "matches" WHERE "matches"."id" | |
#IN ( | |
# SELECT MIN("matches"."id") AS min_id FROM "pairs" INNER JOIN "matches" | |
# ON ("pairs"."player_id" = 14 OR "pairs"."partner_id" = 14) AND ("pairs"."id" = #"matches"."first_pair_id" OR "pairs"."id" = "matches"."second_pair_id") AND "matches"."winner_pair" IS NULL AND #"matches"."wo_pair" IS NULL AND ("matches"."start_date" IS NULL OR "matches"."start_date" >= '2011-10-30 #21:09:48.398210') GROUP BY "matches"."category_id")" | |
class Player < ActiveRecord::Base | |
def planning_matches | |
matches = Match.arel_table | |
pairs = Pair.arel_table | |
condition = (pairs[:player_id].eq(self.id).or(pairs[:partner_id].eq(self.id))) | |
.and(pairs[:id].eq(matches[:first_pair_id]).or(pairs[:id].eq(matches[:second_pair_id])) | |
.and(matches[:winner_pair].eq(nil).and(matches[:wo_pair].eq(nil))) | |
.and(matches[:start_date].eq(nil).or(matches[:start_date].gteq(Time.now))) | |
) | |
Match.where(:id => Pair.select(matches[:id].minimum).joins(:matches).on(condition).group(matches[:category_id])) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment