Help with associations in Rails
Match.find(1).players_home.find(1).Summoner.name
to extract which summoner played player 1 in home team.
The point is: each player in each match can play with different summoner.
I hope I described it clearly.
Regards.I'm not really sure about all your specifications regarding when an association is one or several, but I think something like this could be it:
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
In this setup every match has several participations. Every participation belongs to the player that is participating and also belongs to a summoner for that player and match. It can then be utilized perhaps like this:
In Controller
@match = Match.find(1)
@home_participations = @match.participations.home
@away_participations = @match.participations.away
In View
<h1>Home Players</h1>
<% @home_participations.each do |p| %>
<p>Player: <%= p.player.name %>, Summoned by: <%= p.summoner.name %></p>
<% end %>
I hope this was at least somewhat what you where going for. Let me know if you are looking for something else.
精彩评论