我正在构建一个类似体育联盟事件的web应用程序。有3个模型,它们都是相互关联的。玩家(用户)、团队和游戏。球队有很多球员,也有很多比赛。玩家有许多游戏,并且可以有多个团队。游戏属于团队和玩家。
Teams: have_many :players
Teams: have_many :games
Players: have_many :teams
Players: have_many :games
Games: belong_to :teams
Games: belong_to :players有没有一种方法可以使用"has_many Through表“创建”三重关联“?我倾向于"has_many through“,然后在"through”表中跟踪每个玩家的响应状态。我还希望两支球队都在“直通”表中,这样我就可以做一些类似(player_id,team_id,game_id)的事情,并且只为两支球队创建一场比赛。
或者我上面所说的都是有效的?还是我完全疯了,走错了路?
发布于 2015-05-05 14:24:22
您可以尝试这样做:
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希望这能对你有所帮助。
https://stackoverflow.com/questions/30045333
复制相似问题