I'm building a sports league evite like web application. There are 3 models, all related to each other. Players (users), Teams, and Games. Teams have many players, and many games. Players have many games, and can have multiple teams. Games belong to teams and players.
Teams: have_many :players
Teams: have_many :games
Players: have_many :teams
Players: have_many :games
Games: belong_to :teams
Games: belong_to :players
Is there a way to create a "triple association" using a "has_many through" table? I'm leaning towards a "has_many through" and then tracking the status of each player's response in the "through" table. I'd also like to have both teams in the "through" table, that way I can do something like (player_id, team_id, game_id) and only create one game for both teams.
Or does what I have above work? Or am I completely off my rocker and going about this the wrong way?
You can try this:
class Game < ActiveRecord::Base
belongs_to :gameable, polymorphic: true
end
class Team < ActiveRecord::Base
has_many :games, as: :gameable
has_many :players, through: :matches
end
class Player < ActiveRecord::Base
has_many :games, as: :gameable
has_many :teams, through: :matches
end
class Match < ActiveRecord::Base
belongs_to :players
belongs_to :teams
end
Hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.