2011-03-17 95 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