簡體   English   中英

has_many關聯

[英]has_many associations

所以,我的問題是:如何使調用成為可能:

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

在您的氏族課程中:

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

其他基本相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM