测验应用程序的Rails关联和数据库设计

我正在构建此测验应用程序。 我希望它有点复杂。

我想出了这个数据库模式。 但我真的很困惑..对于我需要什么样的关联和东西感到困惑。

嗯..有一点需要注意的是,当创建测试时,没有关于将接受测试的候选人数量的信息。 因此,我将test_questionscandidate_answers创建为单独的表。

请帮助我协会。

在此处输入图像描述

让我们看看,那将是:

 # For Questions: :has_many => :answers :belongs_to => :test_question # questions table should have the "test_question_id" column # Answers: :has_many => :candidate_answers :belongs_to => :question # Answers table should have the "question_id" column #Test Questions: :has_many => :questions :has_many => :candidate_answers :belongs_to => :test # test questions table should have the "test_id" column but not the "question_id" #Tests: :has_many => :results :has_many => :test_questions :has_many => :candidates, :through => :results, :foreign_key => "candidate_id" #why not? ^^ #Results :belongs_to => :test :belongs_to => :candidate # Results table should have the "test_id" and "candidate_id" columns #candidate_answers :belongs_to => :candidate :belongs_to => :test_question :belongs_to => :answer # candidate_answers table should have the "test_question_id", "candidate_id" and "answer_id" columns #Candidates :has_many => :candidate_answers :has_many => :results :has_many => :answered_tests, :class_name => "test", :through => :results, :foreign_key => "test_id" # again, why not? 

根据您提供的信息,您应该按照自己的意愿行事。 ;)

这应该做:

  • 删除candidate_answers.test_question
  • 删除candidate_answers
  • 删除test_questions

    class Test

    class Result

    class Candidate

    class Answer

    class Question

看起来你打算重复使用不止一个问题的答案,这通常是一个坏主意。在这种情况下更好地克隆答案。