has_many associations
So, my question is: how to make possible that calls:
Clan.win #to get all won rounds
Clan.blue #to get all rounds when clan was in blue team
or even this:
Clan.matches_win #need to calculate which team won match by counting rounds
Clan.matches_lost
Round model:
class Round < ActiveRecord::Base
belongs_to :match
has_and_belongs_to_many :ban开发者_运维知识库ned_champions, :class_name => "Champion", :join_table => "banned_champions_rounds"
belongs_to :clan_blue, :class_name => "Clan", :foreign_key => "clan_blue_id"
belongs_to :clan_purple, :class_name => "Clan", :foreign_key => "clan_purple_id"
belongs_to :winner, :class_name => "Clan", :foreign_key => "winner_id"
end
Clan model:
class Clan < ActiveRecord::Base
has_many :players
has_many :rounds
end
Match model:
class Match < ActiveRecord::Base
has_many :rounds
end
In your Clan class:
def win
rounds.where("winner_id = ?", self.id)
end
The others are more or less the same.
精彩评论