2011-03-18 208 views
0

所以,我的问题是:如何使可能的调用:has_many关联

Clan.win #to get all won rounds 
Clan.blue #to get all rounds when clan was in blue team 

,甚至这样的:

Clan.matches_win #need to calculate which team won match by counting rounds 
Clan.matches_lost 

回合模式:

class Round < ActiveRecord::Base 
    belongs_to :match 
    has_and_belongs_to_many :banned_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 

族模型:

class Clan < ActiveRecord::Base 
    has_many :players 
    has_many :rounds 
end 

比赛模式:

class Match < ActiveRecord::Base 
    has_many :rounds 
end 

回答

1

在你的战队等级:

def win 
    rounds.where("winner_id = ?", self.id) 
end 

其他人都或多或少相同。