-
-
Save sevenseacat/6879288 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Match < ActiveRecord::Base | |
belongs_to :p1, class_name: 'Player' | |
belongs_to :p2, class_name: 'Player' | |
def find_by_player(player) | |
where('p1_id = :player OR p2_id = :player', player: player.id) | |
end | |
def opponent_of(player) | |
if p1 == player ? p2 : p1 | |
end | |
end |
This file contains hidden or 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
class Player < ActiveRecord::Base | |
def matches | |
Match.find_by_player(self) | |
end | |
def opponents | |
matches.collect { |m| m.opponent_of(self) } | |
end | |
end |
This file contains hidden or 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
ActiveRecord::Schema.define(version: 20131006225214) do | |
create_table "matches", force: true do |t| | |
t.integer "result" | |
t.boolean "checked_out", default: false | |
t.boolean "repeated", default: false | |
t.boolean "planned", default: true | |
t.integer "round" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
t.integer "p1_id" | |
t.integer "p2_id" | |
end | |
create_table "players", force: true do |t| | |
t.integer "in_tournament_id" | |
t.boolean "byed", default: false | |
t.integer "matches", default: 0 | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
end | |
create_table "tournaments", force: true do |t| | |
t.boolean "allow_repeat", default: false | |
t.boolean "locked", default: false | |
t.integer "number_players" | |
t.integer "rounds" | |
t.integer "matches_per_round" | |
t.integer "additional_rounds" | |
t.integer "round", default: 0 | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment