记录运动队的比赛 - 多对多关系?

发布于 2024-10-17 07:12:51 字数 408 浏览 2 评论 0原文

我是 Ruby on Rails 的新手,但我过去创建过一些简单的应用程序。现在我正在做一些更复杂的事情,并且我对数据库设计感到困惑。

我正在创建一名体育联盟经理,我需要一些关于如何建模团队和比赛之间关系的建议,以指导我正确的方向。每次两队之间进行比赛时,都会记录比赛情况。我希望能够执行以下操作:

1)在特定球队的页面上,我想显示该球队参加过的比赛列表。2

)我想保留每支球队的胜负记录,并在联赛积分榜页面上显示并列。

关于第一点,我认为这将是多对多的关系,也就是说,一个团队有很多场比赛,一场比赛有很多场比赛(好吧,实际上只有两个)。我有点困惑的是如何以及在哪里存储每个团队的统计数据。我在哪里保存胜利/失败/平局?它们是团队表的一部分吗?如果是这样,如果我有一个包含球队排名的页面,显示每个球队的损失/平局,我将如何获得该信息?

I am new to Ruby on Rails, but I have created a few simple apps in the past. Now I am doing something a little more complex and I am stumped on database design.

I am creating a sports league manager and I need some advice on how the relationship between teams and games is modelled to point me in the right direction. Every time a game is played between two teams, the match is recorded. I'd like to be able to do the following:

1) On a specific team's page, I would like to show a list of matches the team has participated in.

2) I would like to keep a record of each team's wins, losses, and ties to show on a league standings page.

On point #1 I figured this would be a many-to-many relationship, that is, a Team has many Matches, and a Match has many Teams (well, just two actually). The point I am a bit stumped on is how and where to store the stats for each team. Where do I keep the wins/losses/ties? Are they part of the Team table? If so, if I was to have a page with team standings showing each teams w/losses/ties, how would I get that information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天涯沦落人 2024-10-24 07:12:51

这还没有真正完成,但也许这会帮助您或其他人开始工作。

我专注于如何构建团队和比赛之间的关系。我相信,至少部分解决方案在于使用多态关联,其中一部分可能是自连接。我发誓它就在我面前,但我却没有看到它。

这里先一步一步,假设您的 Matches 表有一个这样的表……

  id   |   home_team_id  |  away_team_id  |  home_team_score  |  away_team_score

您可以在模型中使用这些关联来设置它:

class Match
  belongs_to :home_team, :class_name => :team
  belongs_to :away_team, :class_name => :team
end

class Team
  has_many :home_matches, :foreign_key => :home_team_id, :class_name => :matches
  has_many :away_matches, :foreign_key => :away_team_id, :class_name => :matches
end

明显的问题是,实际上应该只有一种关系,但存在两种关系。这就是为什么我认为多态关联可以有所帮助,但这有点令人费解。

请参阅有关 多态关联 的 Rails 指南,看看这是否可以帮助您了解我的能力't。

This isn't really finished, but maybe this will help you or someone else get the ball rolling here.

I'm focusing on just how to structure the relationship between Teams and Matches. At least part of the solution lies in using a polymorphic association, I believe, and part of it would perhaps be a self join. I swear it's right in front of me and I'm not seeing it.

Taking baby steps here, assuming you have a table like this for your Matches table…

  id   |   home_team_id  |  away_team_id  |  home_team_score  |  away_team_score

You can set that up in your models with these associations:

class Match
  belongs_to :home_team, :class_name => :team
  belongs_to :away_team, :class_name => :team
end

class Team
  has_many :home_matches, :foreign_key => :home_team_id, :class_name => :matches
  has_many :away_matches, :foreign_key => :away_team_id, :class_name => :matches
end

The obvious problem there is that there are two relationships when there really should only be one. That's why I think a polymorphic association can help, but this is sort of convoluted.

See the Rails guide on polymorphic associations and see if that helps you see what I can't.

知足的幸福 2024-10-24 07:12:51

我建议不要在这里创建传统的多对多关系。相反,您只有两个表:TeamsMatches

每个团队将由 Teams 中的一行进行标识,并且具有唯一的标识符,例如 TeamId

Matches 表包含以下列:

  • MatchId - 合成主键
  • SeasonId - 标识比赛发生在
  • HomeTeamId< 的 赛季/code> - 主队
  • VisitngTeamId - 客队
  • HomeTeamScore
  • VisitngTeamScore
  • ... 您想要保留的任何其他统计数据对于个人比赛

我想您已经有了主队和客队的概念。如果没有,您可以将这些列命名为 Team1IdTeam2Id,或者类似的名称。

我有点困惑的是如何以及在哪里存储每个团队的统计数据。我在哪里保存胜利/失败/平局?

胜利、失败和平局都隐含在 Matches 表中 - 您可以查询该表以获取球队的记录。例如,以下查询返回 X 队的获胜、失败和平局:

-- Wins
SELECT COUNT(*) 
FROM Matches 
WHERE SeasonID = @SeasonID AND 
    (HomeTeamId = X AND HomeTeamScore > VisitingTeamScore) OR 
    (VisitingTeamId = X AND VisitingTeamScore > HomeTeamScore)

-- Loses
SELECT COUNT(*) 
FROM Matches 
WHERE SeasonID = @SeasonID AND 
    (HomeTeamId = X AND HomeTeamScore < VisitingTeamScore) OR 
    (VisitingTeamId = X AND VisitingTeamScore < HomeTeamScore)

-- Ties
SELECT COUNT(*) 
FROM Matches 
WHERE SeasonID = @SeasonID AND 
    (HomeTeamId = X OR VisitingTeamId = X) 
    AND VisitingTeamScore = HomeTeamScore

即使您想要对数据模型进行非规范化并存储每个团队的此信息,您也不会希望在 Teams< /code> 表,因为您可能想知道一支球队在给定赛季中有多少胜/负/平局。 (我认为一支球队可能会在多个赛季中团结在一起。如果不是这种情况,请忽略。)

I would suggest against creating a traditional many-to-many relationship here. Instead, you'd have just two tables: Teams and Matches.

Each team would be identified by a row in Teams and would have a unique identifier, such as TeamId.

The Matches table would the following columns:

  • MatchId - a synthetic primary key
  • SeasonId - identifies the season the match took place in
  • HomeTeamId - the home team
  • VisitngTeamId - the away team
  • HomeTeamScore
  • VisitngTeamScore
  • ... Any other statistics you'd want to keep for an individual match

I presume you have the notion of home and visiting teams. If not, you can just name these columns Team1Id and Team2Id, or something along those lines.

The point I am a bit stumped on is how and where to store the stats for each team. Where do I keep the wins/losses/ties?

The wins, losses, and ties are implicit in the Matches table - you can query that to get back a team's record. For instance, the following query returns the wins, loses, and ties for team X:

-- Wins
SELECT COUNT(*) 
FROM Matches 
WHERE SeasonID = @SeasonID AND 
    (HomeTeamId = X AND HomeTeamScore > VisitingTeamScore) OR 
    (VisitingTeamId = X AND VisitingTeamScore > HomeTeamScore)

-- Loses
SELECT COUNT(*) 
FROM Matches 
WHERE SeasonID = @SeasonID AND 
    (HomeTeamId = X AND HomeTeamScore < VisitingTeamScore) OR 
    (VisitingTeamId = X AND VisitingTeamScore < HomeTeamScore)

-- Ties
SELECT COUNT(*) 
FROM Matches 
WHERE SeasonID = @SeasonID AND 
    (HomeTeamId = X OR VisitingTeamId = X) 
    AND VisitingTeamScore = HomeTeamScore

Even if you wanted to denormalize the data model and store this information for each team, you wouldn't want to do it in the Teams table because you may want to know how many wins/losses/ties a team has for a given season. (I presume a team may stick together through multiple seasons. If this is not the case, disregard.)

南汐寒笙箫 2024-10-24 07:12:51

我在这里即兴发言,但请考虑一下:

tblTeam
    TeamID
    TeamName
     . . . OtherTeamFields

tblMatch
    MatchID
    MatchDate
    MatchLocationID
     . . . OtherMatchFields

tblTeam_Matches
    TeamID FK on tblTeam.TeamID
    MatchID FK on tblMatchID
    TeamStanding (Win, Loss, Tie)

上面的结构有一些优点和缺点。从专业角度来看,参与比赛的每个球队的结果都与球队与该比赛的关系一起正确存储。人们可以通过设置 TeamID 和 TeamStanding 的条件(即“WHERE TeamStanding = “Win”)来检索一系列比赛中每支球队的结果。

但是,有一种更复杂但可能更具可扩展性和有用的方法,您可以在其中为 tblTeam_Matches 定义一个 TeamScore 字段,在这种情况下,比赛获胜者将由一系列相当困难的子查询决定(困难,我的意思是对我来说困难。我敢打赌这里有人可以弹出一个 smaple)。很快就出来了。但这有点令人费解)。

我相信第二种选择是更“正确”的方法,但我搞砸了一些版本:

StatsQuery:
    TeamName
    TotalMatches
    Wins
    Losses
    Ties

我度过了一段艰难的时光。 无论如何,相关子查询还不是我的强项,

希望能给你一些思考。

I'm going off-the-cuff here, but consider:

tblTeam
    TeamID
    TeamName
     . . . OtherTeamFields

tblMatch
    MatchID
    MatchDate
    MatchLocationID
     . . . OtherMatchFields

tblTeam_Matches
    TeamID FK on tblTeam.TeamID
    MatchID FK on tblMatchID
    TeamStanding (Win, Loss, Tie)

The structure above has some pros and cons. On the pro side, the outcome for each team involved in a match is stored properly with the team's relationship to that match. One can retrieve the results for each team through a range of matches by setting criteria for TeamID and TeamStanding (i.e. "WHERE TeamStanding = "Win").

However, there is a more complex, but probably more scalable and usefull way, where you would define a TeamScore field for tblTeam_Matches. In this case, the Match winner would be determined by a rather difficult series of sub-queries (By difficult, I mean difficult for ME. I am betting there are folks here on SO who could pop a smaple out quickly . . . But it is a bit of a brain-teaser).

I believe the second option would be a more "proper" way to do it, but I messed with puling some version of:

StatsQuery:
    TeamName
    TotalMatches
    Wins
    Losses
    Ties

I had a difficult time of it. Correlated sub-queries are NOT my strong point (yet).

Anyway, hope that gives you some food for thought . . .

手心的温暖 2024-10-24 07:12:51

我认为这可以使用以下方式完成

class Team < ActiveRecord::Base
    has_many :games
    has_many :matches,:through => :games
end

class Matche < ActiveRecord::Base
    has_many :games
    has_many :teams,:through => :games
end

class Game < ActiveRecord::Base
    belongs_to :team
    belongs_to :match
end

,如果您需要查找任何球队的总比赛数,那么

= team.games.count

如果您需要查找任何球队的总获胜比赛数,那么

= team.games.where(:winner => true).count

I think this can be done using following way

class Team < ActiveRecord::Base
    has_many :games
    has_many :matches,:through => :games
end

class Matche < ActiveRecord::Base
    has_many :games
    has_many :teams,:through => :games
end

class Game < ActiveRecord::Base
    belongs_to :team
    belongs_to :match
end

and if you need to find total matches played by any team then

= team.games.count

if you need to find total won matches by any team then

= team.games.where(:winner => true).count
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文