【问题标题】:How do I model a table tennis match in rails如何在 Rails 中为乒乓球比赛建模
【发布时间】:2010-10-05 04:53:56
【问题描述】:

我正在尝试在 Rails 中模拟乒乓球比赛。这是我所拥有的:

游戏模型:
team_1_score
team_2_score
team_1_id
team_2_id

团队模型:
游戏ID
player_id

播放器型号:
名称

因此,每场比赛将由 2 支球队组成(每支球队有 1 名或 2 名球员)。
然后我打算用has_many将游戏与玩家联系起来,:through。我认为这不会起作用,因为每场比赛都有 2 个团队实例。但我真的不知道我应该从这里去哪里。任何帮助将不胜感激。

【问题讨论】:

  • 对于Game,您如何确定哪个是team_1,哪个是team_2?你有 home_teamaway_team 或任何其他显着特征吗?
  • 第 1 队和第 2 队是任意的,仅作为将球员分组进行比赛的一种方式。这些团队可以定期且经常更换。

标签: ruby-on-rails database-design rails-models


【解决方案1】:

我不知道如何在玩家和游戏之间进行 has_many :through,但如果你从这样的事情开始可能会更容易:

Team Model
id
name
has_many :players
has_many :games

Player Model
id
name
team_id 
has_one :team

那么 Games 模型将会有类似的东西(除了你已经拥有的):

has_one :team1, :class_name => 'Team'
has_one :team2, :class_name => 'Team'

【讨论】:

  • 我不希望将球员分配到球队,但我确实希望球队由球员组成。我本质上希望能够从球员中创建一支球队。但我会用你给我的东西,看看我能不能弄明白。谢谢
  • 好吧,您可以引入另一个名为“team_players”的表,它只有将球员与球队联系起来的外键。这将使您可以灵活地将球员分配给超过 1 个团队。 (抱歉,我似乎无法正确格式化) TeamPlayer 模型:team_id player_id belongs_to :team belongs_to :player 玩家模型 has_many :teams, :through => :team_players 团队模型 has_many :players, :through => :team_players跨度>
  • 谢谢,我想我现在可以弄清楚了。
猜你喜欢
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多