ruby on rails – Database schema for multiple choice Trivia game

ruby on rails – Database schema for multiple choice Trivia game

I would either:

  1. Keep answers in a single table with a boolean to indicate the correct answer, just as you describe.
    or …
  2. Keep answers in a single table, and use an attribute on the question model to identify the correct answers id. This ensures that only one answer is correct.

In the latter case:

Question
  attr_accessible :question, :level, correct_answer_id
  has_many :answers     , :dependent => :destroy
  has_many :user_answers, :through   => :answers

Answer
  attr_accessible :answer, :question_id
  belongs_to :question
  has_many   :user_answers, :dependent => :destroy

User_Answer
  attr_accessible :user_id, :answer_id
  belongs_to :user
  belongs_to :answer

Over-thinking this a bit, if you had a lot of questions that had the same sets of answers but with a different ones being correct for different questions, then you could bundle up answers into sets (Blue, Green, Red) and link questions to sets — in that case youd certainly have to indicate the correct answer at the question level.

I would do it like this:

a question model

Question
   attr_accessible :question, :level
   has_many wrong_answers
   has_one correct_answer

Wrong_answer
   attr_accessible :answer, :question_id
   belongs_to :question

Correct_answer
   attr_accessible :answer, :question_id
   belongs_to :question

ruby on rails – Database schema for multiple choice Trivia game

Leave a Reply

Your email address will not be published.