Ruby on rails Rails关联和用于测验应用程序的db设计

Ruby on rails Rails关联和用于测验应用程序的db设计,ruby-on-rails,ruby-on-rails-3,database-design,Ruby On Rails,Ruby On Rails 3,Database Design,我正在构建这个测验应用程序。我希望它有点复杂 我提出了这个数据库模式。但是我真的很困惑。。困惑于我需要什么样的联想和东西 嗯。。需要注意的一点是,在创建测试时,没有关于将参加测试的候选人数量的信息。因此,我创建了测试问题和候选答案作为单独的表格 请帮我联系一下 让我们看看,那将是: # For Questions: :has_many => :answers :belongs_to => :test_question # questions table should have

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

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

嗯。。需要注意的一点是,在创建测试时,没有关于将参加测试的候选人数量的信息。因此,我创建了
测试问题
候选答案
作为单独的表格

请帮我联系一下


让我们看看,那将是:

# 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?
有了你提供的信息,你就可以随心所欲了

这应该可以:

  • 放下
    候选者的答案。测试问题
  • 删除
    候选答案
  • 删除
    测试问题

    类测试 类结果 类候选 类答案 类问题

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

事实上,您几乎完成了数据库模式的所有工作,我只是猜测您有一个问题列表,可以为每个独特的测试选择,这就是为什么您有“测试问题”表。这也是为什么有考生答案表的原因,因为每个考生都可以回答每个测试的每个问题。这是真的,它相当复杂,但事实上,如果你有逻辑的话,就不那么复杂了。你应该提供更多的信息对不起。我想我应该解释一下这个模式。很好地理解了逻辑:)再次感谢!