【问题标题】:Implementation of multiple active record associations多个活动记录关联的实现
【发布时间】:2012-06-24 09:41:31
【问题描述】:

我正在尝试为我的 Ruby on Rails 应用程序设置模型,但模型非常复杂——我不确定我是否以正确的方式处理它。阅读 Ruby on Rails 指南 @guides.rubyonrails.org 提供了一些关于 Active Record 关联的非常好的信息,但它在一定程度上也让我更加困惑。我正在尝试设置的是模拟锦标赛而不是传统锦标赛的东西(如果有人遵循 GSL,那就是我想尝试建模的锦标赛)。

锦标赛模型存储以下信息:

  • 姓名
  • 横幅(只是在线托管图片的网址)
  • 地图池(用于锦标赛的地图对象的集合)
  • 选手(参加本次比赛的选手人数)
  • 32强决赛(32、16、8、4、2轮)

这些是我想出的模型:锦标赛、地图池、地图、玩家、种族(即 Protoss 人族或虫族)、团队(即玩家所属的团队)、Round_of_32、Round_of_16、Round_of_8、Round_of_4、 Round_of_2、Group(每轮比赛的集合)、Match(包含玩家之间每场比赛的信息的集合)和 Game(包含地图和获胜者)。随着应用程序的发展,我将拥有越来越多的锦标赛,每个锦标赛都有自己的数据集,即使玩家可能是多个锦标赛的一部分,地图也可能是他们各自锦标赛的多个地图池的一部分,等等。

  • 锦标赛有一个包含许多地图的地图池
  • 一场锦标赛有多个玩家,每个玩家都与不同的种族和团队相关联
  • 32强赛共8组,每组5场,每组3场
  • 16 轮与 32 轮类似,但只有四组
  • 8强赛只有4场比赛,每场5场
  • 4 轮 2 场,每场 5 场
  • 一轮 2 有 1 场比赛,每场比赛 7 场

这是我目前所拥有的:

TOURNAMENT
  :league
  :banner
  has_one :map_pool
  has_many :maps, :through => :map_pool
  has_and_belongs_to_many :players
  # has_many :rounds
  has_one :ro32
  has_one :ro16
  has_one :ro8
  has_one :ro4
  has_one :ro2

MAP_POOL
  belongs_to :tournament
  has_many :maps

MAP
  :name
  belongs_to :map_pool
  has_many :games

PLAYER
  :name
  belongs_to :race
  belongs_to :team
  has_and_belongs_to_many :tournaments
  has_many :games

RACE
  :type {"Protoss", "Terran", "Zerg"}
  has_many :players

TEAM
  :name
  has_many :players

RO32
  belongs_to :tournament
  has_many :groups, :as => :round,
                    :limit => 8

RO16
  belongs_to :tournament
  has_many :groups, :as => :round,
                    :limit => 4

RO8
  belongs_to :tournament
  has_many :matches, :as => :matchup,
                      :limit => 4

RO4
  belongs_to :tournament
  has_many :matches, :as => :matchup,
                      :limit => 2

RO2
  belongs_to :tournament
  has_many :matches, :as => :matchup,
                      :limit => 1

GROUP
  :name
  belongs_to :round, :polymorphic => true
  has_many :matchups, :as => :matchup

MATCH
  :type {"bo3", "bo5", "bo7"}
  has_many :games
  belongs_to :matchup, :polymorphic => true

GAME
  belongs_to :match
  has_one :map
  has_one :player

我是否遗漏了任何东西或错误地使用了任何东西?

编辑:我为每一轮都有单独的模型的原因是因为第 32/16 轮与第 8/4/2 轮不同。以下是一组示例数据,可以解释我遇到的问题:

Round of 32:
    Group A:
        Match 1: P1 vs P2
            Game 1
            Game 2
            Game 3
        Match 2: P3 vs P4
            Game 1
            Game 2
            Game 3
        Winners Match: P1 (winner match 1) vs P4 (winner match 2)
            Game 1
            Game 2
            Game 3
        Losers Match: P2 (loser match 1) vs P3 (loser match 2)
            Game 1
            Game 2
            Game 3
        Tiebreak Match: P4 (loser of winners match) vs P2 (winner of losers match)
            Game 1
            Game 2
            Game 3
    [etc. Group B through Group H]
Round of 16:
    [similar to Round of 32 but only groups A through D]
Round of 8:
    Match 1: P1 vs P2
        Game 1
        Game 2
        Game 3
        Game 4
        Game 5
    Match 2: P3 vs P4
        Game 1
        Game 2
        Game 3
        Game 4
        Game 5
    Match 3: P5 vs P6
        Game 1
        Game 2
        Game 3
        Game 4
        Game 5
    Match 4: P7 vs P8
        Game 1
        Game 2
        Game 3
        Game 4
        Game 5

【问题讨论】:

    标签: ruby-on-rails activerecord associations


    【解决方案1】:

    我还是会说你的子弹应该是一个单一的模型。似乎它们类型之间的差异是逻辑上的而不是结构上的。所以我会做一个圆桌会议并添加类型属性(利用单表继承?)。

    作为一名 SC 粉丝,如果您愿意,我很乐意帮助您进行建模。

    还有:玩家>-- 种族?你确定吗?我无法在游戏甚至锦标赛之间更改比赛?!

    【讨论】:

    • 非常感谢您提供有关圆形模型的提示。此外,对于玩家/种族问题,您提出了一个很好的观点。我只是认为这会使搜索变得更容易一些(即,不必搜索所有类型为“RACE”的玩家,在更改玩家的种族时,每场比赛都有一张桌子可能需要更多的工作,但它会使查找速度更快一些)
    • erm,我认为你不理解我的比赛建议。我的结构是这样的。 player >- participates -< rounds 和参与表将保存有关种族的数据。 (这也将允许未来 1+ v 1+ 游戏)。
    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多