2011-03-17 93 views
0


讓我來描述一下我想做的事:
有匹配模式,它應該有什麼球員,出席了在它什麼氏族的信息,以居家球員分工和宗族客場球員和戰隊。
這很簡單,但有另一種模式:召喚師。在每場比賽中,每名球員都有不同的召喚師,我需要做這樣的事情:Match.find(1).players_home.find(1).Summoner.name來提取哪位召喚師在主隊參加了比賽。
重點是:每場比賽中的每名球員都可以使用不同的召喚師進行比賽。
我希望我描述清楚。
此致敬禮。幫助與Rails的協會

+1

那麼問題是什麼?你有一些代碼給我們看? – Wukerplank 2011-03-17 10:21:17

+0

問題是如何設置關聯和模型。 – methyl 2011-03-17 10:31:51

回答

1

我真的不知道你所有的規格就當一個關聯是一個或幾個,但我覺得這樣的事情可能是它:

class Match 
    has_many :participations 
    has_many :players, :through => :participations 
end 

class Participation 
    belongs_to :match 
    belongs_to :player 
    belongs_to :summoner 

    # also a team attribute to store either "home" or "away" 
    scope :home, where(:team => "home") 
    scope :away, where(:team => "away") 
end 

class Player 
    belongs_to :clan 
    has_many :participations 
    has_many :matches, :through => :participations 
end 

class Summoner 
    has_many :participations 
end 

在此設置每場比賽有幾個參股。每一次參與都屬於參與的玩家,並且也屬於該玩家的召喚者和匹配。然後它可以或許利用這樣的:

在控制器

@match = Match.find(1) 
@home_participations = @match.participations.home 
@away_participations = @match.participations.away 

在查看

<h1>Home Players</h1> 
<% @home_participations.each do |p| %> 
    <p>Player: <%= p.player.name %>, Summoned by: <%= p.summoner.name %></p> 
<% end %> 

我希望這至少是有點什麼你要去的地方。讓我知道你是否在尋找別的東西。

+0

非常棒的解決方案,謝謝! – methyl 2011-03-17 11:58:58