我正在设计足球统计应用程序。我坚持存储游戏(比赛)结果。我有 Team 对象和 Game 对象。首先我让游戏模型看起来像
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
但看起来球队的顺序与分数不匹配,而且看起来很丑。接下来我创建了模型 Score 用于存储团队及其得分,而 Game 模型有很多 Score,但这比以前的更难看。