Skip to content

Instantly share code, notes, and snippets.

@robertomiranda
Created August 24, 2011 15:34
Show Gist options
  • Save robertomiranda/1168322 to your computer and use it in GitHub Desktop.
Save robertomiranda/1168322 to your computer and use it in GitHub Desktop.
Find tournament's players by categories
class Tournament < ActiveRecord::Base
#SELECT "players".*
#FROM "players"
#WHERE "players"."id" IN (
#SELECT "players"."id"
#FROM "players"
#INNER JOIN "pairs"
#ON ("pairs"."player_id" = "players"."id" OR "pairs"."partner_id" = "players"."id") AND "pairs"."tournament_id" = 4)
def player
players = Player.arel_table
pairs = Pair.arel_table
condition = (pairs[:player_id].eq(player[:id]).or(pairs[:partner_id].eq(player[:id]))).and(pairs[:tournament_id].eq(self.id))
Player.where(:id => Player.select(players[:id]).joins(:pairs).on(condition))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment