Ruby on rails 如何在MongoDB中存储足球队、比赛和分数
我正在设计足球统计应用程序。我坚持保存比赛结果。 我有团队目标和游戏目标。首先,我让游戏模型看起来像Ruby on rails 如何在MongoDB中存储足球队、比赛和分数,ruby-on-rails,ruby,mongodb,database-design,mongoid,Ruby On Rails,Ruby,Mongodb,Database Design,Mongoid,我正在设计足球统计应用程序。我坚持保存比赛结果。 我有团队目标和游戏目标。首先,我让游戏模型看起来像 class Game include Mongoid::Document belongs_to :team_1, class_name: "Team" belongs_to :team_2, class_name: "Team" field :score_1, type: Integer field :score_2, type: Integer 但它不允许我找到所
class Game
include Mongoid::Document
belongs_to :team_1, class_name: "Team"
belongs_to :team_2, class_name: "Team"
field :score_1, type: Integer
field :score_2, type: Integer
但它不允许我找到所有的团队游戏。 接下来,我决定做如下事情:
class Game
include Mongoid::Document
has_and_belongs_to_many :teams
field :scores, type: Array
class Game
include Mongoid::Document
has_many :teams #I don't get why you would need HABTM
embeds_one :score
end
class Team
include Mongoid::Document
belongs_to :game
has_many :scores
end
class Score
include Mongoid::Document
embedded_in :game
belongs_to :team
field :score, type: Integer
end
class Game
class InvalidScorerError < Exception ; end
include Mongoid::Document
belongs_to :home_team, class_name: "Team"
belongs_to :away_team, class_name: "Team"
embeds_many :goals
field :home_team_goals, type: Integer, default: 0
field :away_team_goals, type: Integer, default: 0
def register_a_goal(team)
if team == home_team
self.home_team_goals += 1
self.save!
elsif team == away_team
self.away_team_goals += 1
self.save!
else
raise InvalidScorerError, "Team should be either home or away!"
end
end
def total_match_goals
self.home_team_goals + self.away_team_goals
end
end
class Team
include Mongoid::Document
has_many :inhouse_games, class_name: "Game", inverse_of: :home_team
has_many :games_as_a_visitor, class_name: "Game", inverse_of: :away_team
end
但看起来球队的顺序不是比赛分数,而且看起来很难看。
接下来,我创建了用于存储团队和it分数的模型分数,游戏模型有很多分数,但这比前一个更难看。我认为一个游戏有两个团队(或者更多,idk这是什么类型的游戏),并且有一个分数(没有游戏就不存在)。不过,您需要知道哪个分数与哪个团队相关,因此它们之间存在关系,因此,您的设计如下所示:
class Game
include Mongoid::Document
has_and_belongs_to_many :teams
field :scores, type: Array
class Game
include Mongoid::Document
has_many :teams #I don't get why you would need HABTM
embeds_one :score
end
class Team
include Mongoid::Document
belongs_to :game
has_many :scores
end
class Score
include Mongoid::Document
embedded_in :game
belongs_to :team
field :score, type: Integer
end
class Game
class InvalidScorerError < Exception ; end
include Mongoid::Document
belongs_to :home_team, class_name: "Team"
belongs_to :away_team, class_name: "Team"
embeds_many :goals
field :home_team_goals, type: Integer, default: 0
field :away_team_goals, type: Integer, default: 0
def register_a_goal(team)
if team == home_team
self.home_team_goals += 1
self.save!
elsif team == away_team
self.away_team_goals += 1
self.save!
else
raise InvalidScorerError, "Team should be either home or away!"
end
end
def total_match_goals
self.home_team_goals + self.away_team_goals
end
end
class Team
include Mongoid::Document
has_many :inhouse_games, class_name: "Game", inverse_of: :home_team
has_many :games_as_a_visitor, class_name: "Game", inverse_of: :away_team
end
我不知道你到底想建立什么样的模型,但在我看来,你的模型应该反映你所模拟的游戏本质的真实性 因此,考虑到这种方法,您的类设计应该如下所示:
class Game
include Mongoid::Document
has_and_belongs_to_many :teams
field :scores, type: Array
class Game
include Mongoid::Document
has_many :teams #I don't get why you would need HABTM
embeds_one :score
end
class Team
include Mongoid::Document
belongs_to :game
has_many :scores
end
class Score
include Mongoid::Document
embedded_in :game
belongs_to :team
field :score, type: Integer
end
class Game
class InvalidScorerError < Exception ; end
include Mongoid::Document
belongs_to :home_team, class_name: "Team"
belongs_to :away_team, class_name: "Team"
embeds_many :goals
field :home_team_goals, type: Integer, default: 0
field :away_team_goals, type: Integer, default: 0
def register_a_goal(team)
if team == home_team
self.home_team_goals += 1
self.save!
elsif team == away_team
self.away_team_goals += 1
self.save!
else
raise InvalidScorerError, "Team should be either home or away!"
end
end
def total_match_goals
self.home_team_goals + self.away_team_goals
end
end
class Team
include Mongoid::Document
has_many :inhouse_games, class_name: "Game", inverse_of: :home_team
has_many :games_as_a_visitor, class_name: "Game", inverse_of: :away_team
end
类游戏
类invalidScoreError<异常;结束
include Mongoid::Document
属于:主队,班名:“队”
属于:客场球队,班级名称:“球队”
包含很多:目标
字段:家庭\团队\目标,类型:整数,默认值:0
字段:客场\团队\目标,类型:整数,默认值:0
def注册a_目标(团队)
如果团队==主队
self.home\u团队目标+=1
自救!
elsif团队==客场团队
自我客场\团队\目标+=1
自救!
其他的
升起InvalidCorererror,“团队应该在家或在外!”
结束
结束
def总匹配目标
主场\团队\目标+客场\团队\目标
结束
结束
班队
include Mongoid::Document
有很多:室内比赛,班名:“比赛”,反向的::主场队
有很多:游戏作为访客,课程名称:“游戏”,与客场球队相反
结束
编辑:还有其他的事情要考虑,比如冠军,时间表…想象一下现实生活中的事情是怎么发生的。您的代码只是对现实的抽象
希望有帮助 “但它不允许我找到所有的团队游戏”-为什么不?我需要HABTM’当然一个teem有很多游戏(多对多关系)。第二个问题是我在一场比赛中需要两个得分(我的错,得分意味着进球数)。正如我所知,MongoId无法使模型属于嵌入式模型。我不想指定每个目标的详细信息,只是每个团队的目标数。我认为,获取特定团队的所有游戏的查询有点难看。为什么它只是一个没有特殊性的常规查询<代码>内部游戏+作为访客的游戏