测验应用程序的Rails关联和数据库设计 [英] Rails Associations and db design for Quiz Application

查看:165
本文介绍了测验应用程序的Rails关联和数据库设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立这个测验应用程序。我想要它有点复杂。



我已经想出了这个数据库模式。但是我真的很困惑...对我将需要和什么协会感到困惑。



嗯..有一件事要注意的是,当一个测试被创建没有有关候选人人数的资料。所以,我创建了 test_questions candidate_answers 作为单独的表。



请帮助我协会。



解决方案

我们来看看, / p>

 #对于问题:
:has_many => :答案
:belongs_to => :test_question
#问题表应该有test_question_id列

#答案:
:has_many => :candidate_answers
:belongs_to => :问题
#答案表应该有question_id列

#Test问题:
:has_many => :问题
:has_many => :candidate_answers
:belongs_to => :test
#测试问题表应该有test_id列而不是question_id

#Tests:
:has_many => :results
:has_many => :test_questions
:has_many => :候选人,通过=> :results,:foreign_key => candidate_id#why not? ^^

#Results
:belongs_to => :test
:belongs_to => :候选人
#结果表应该有test_id和candidate_id列

#candidate_answers
:belongs_to => :候选人
:belongs_to => :test_question
:belongs_to => :answer
#candidate_answers表应该有test_question_id,candidate_id和answer_id列

#Candidates
:has_many => :candidate_answers
:has_many => :results
:has_many => :answers_tests,:class_name => test,:through => :results,:foreign_key => test_id#再次,为什么不呢?

随着您提供的信息,这应该做你想要的。 ;)


I am building this quiz application. I want it to be kinda sophisticated.

I have come up with this database schema. But I am really confused.. Confused about what associations I would be needing and stuff.

Well.. one thing to note is, when a test is created there is no information regarding the number of candidates who will take it. So, I created the test_questions and candidate_answers as separate tables.

Please help me with the associations.

解决方案

Let's see, that would be:

# 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?

And with the information you gave, that should do what you want. ;)

这篇关于测验应用程序的Rails关联和数据库设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆