【问题标题】:Database setup and design for 15 questions game - PostgreSQL and Rails15题游戏的数据库设置和设计——PostgreSQL和Rails
【发布时间】:2014-11-07 19:15:48
【问题描述】:

我正在开发一个小游戏,人们可以在其中创建 15 个问题测验。我正在努力弄清楚如何构建我的数据库以获得最佳优化。每个问题都有一个分配给它们的点值(与谁想成为百万富翁的结构相同)。我正在争论是将所有内容都放在同一张桌子上还是将游戏与问题分开(或其他任何方法,如果有更有效的方法)。

策略 1: 为游戏表中的每个创建列。所以会有标题栏,100分问题,100分答案,200分问题,200分答案等。

策略 2: 为问题创建一个新表。每个问题都属于一个游戏,并分配了分值和答案

策略 3: 为每个点值创建许多表。会有一个 100 个表、一个 200 个表等。这些列将只是问题或答案。

这些是实现这款游戏的最有效方式吗?我正在尝试了解数据库设置如何影响速度。

【问题讨论】:

    标签: ruby-on-rails database postgresql database-design database-schema


    【解决方案1】:

    大部分时间效率不如标准化重要。你应该有一个单独的游戏、问答表。你的模型看起来像:

    class Game
      has_many :questions
    end
    
    class Question
      belongs_to :game
      has_many :answers
    end
    
    class Answer
      belongs_to :question
    end
    

    【讨论】:

    • 每场比赛有一个100分的问题,一个200分的问题,一个500分的问题,等等(总共20个)。做has_one :100question,has_one :200 question会好吗?
    • 或者像你的评论那样做会更好,但模型中有范围
    • @DavidMckee 最好为每个问题分配分数,并且,是的,使用范围。您甚至可以添加验证以确保每个点级别只有一个问题。
    【解决方案2】:

    我同意 Damien Roche 的回答,但在这里补充一下我将如何构建您的架构:

    create_table "Game" do |t|
      t.string   "name"
      t.string   "description"
      t.integer  "user_id"    # if you want to keep track of who created it
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    
    create_table "Question" do |t|
      t.string   "description"
      t.integer  "points"
      t.integer  "game_id"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    
    create_table "Answer" do |t|
      t.string   "description"
      t.integer  "question_id"
      t.integer  "game_id"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    

    您可能希望添加一个单独的模型(以及数据库中的相应表)来跟踪正在玩的游戏实例(称为GameRound 或类似名称)。在它的 game_round.rb 文件中看起来像这样:

    class GameRound < ActiveRecord::Base
      belongs_to :game
      belongs_to :user      # the person playing the game 
    

    (您可以添加一个模型来跟踪每个问题的答案,或者在数据库中添加一个属性来跟踪字符串中的相同内容)。

    GameRound 的架构可能是这样的:

    create_table "GameRound" do |t|
      t.integer  "user_id"   # player
      t.integer  "game_id"   # id of the game being played
      t.integer  "score"     # accumulation of points
      t.string   "answers"   # a string of the answers to each question ex: "a,c,a,b,d"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2011-09-06
      • 1970-01-01
      • 2012-01-31
      • 2013-12-03
      相关资源
      最近更新 更多