我正在构建这个测验应用程序。我希望它有点复杂。
我想出了这个数据库模式。但我真的很困惑..对我需要什么关联和东西感到困惑。
嗯.. 需要注意的一件事是,在创建测试时,没有关于将要参加该测试的候选人数量的信息。所以,我创建了test_questions
andcandidate_answers
作为单独的表。
请帮助我与协会。
我正在构建这个测验应用程序。我希望它有点复杂。
我想出了这个数据库模式。但我真的很困惑..对我需要什么关联和东西感到困惑。
嗯.. 需要注意的一件事是,在创建测试时,没有关于将要参加该测试的候选人数量的信息。所以,我创建了test_questions
andcandidate_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
桌子
类测试 < ActiveRecord::Base has_many :results has_many :questions end
类结果 < ActiveRecord::Base belongs_to :test belongs_to :candidate end
类候选人 < ActiveRecord::Base has_many :results has_many :answers end
类答案 < ActiveRecord::Base belongs_to :question belongs_to :candidate end
类问题 < ActiveRecord::Base belongs_to :test has_many :answers end
看起来您打算将答案重复用于一个以上的问题,这通常是一个坏主意。在这种情况下,最好克隆一个答案。