2012-12-21 22 views
2

我正在構建此測驗應用程序。我希望它有點複雜。測驗應用程序的Rails關聯和db設計

我已經想出了這個數據庫模式。但我真的很困惑..我會困惑什麼協會需要和東西。

呃..有一點需要注意的是,當創建一個測試時,沒有關於將要使用它的候選人數量的信息。所以,我創建了test_questionscandidate_answers作爲單獨的表格。

請幫助我的協會。

enter image description here

回答

1

讓我們來看看,這將是:

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

而且隨着你給的信息,應該做你想做的。 ;)

+0

謝謝你:)非常多 –

+0

事實上,你幾乎完成了數據庫模式的所有工作,我只是猜測你有一個問題列表,你可以選擇每個獨特的測試,爲什麼你有「test_questions」表。所以這也是爲什麼你有candidate_answers表,因爲每個考生可以回答每個測試的每個問題。確實如此,這很複雜,但事實上並非如此。你應該多給點關於這方面的信息。 ;-) – Kulgar

+0

對不起。我想我應該解釋一下模式。讀完邏輯後,又做得很好:)再次感謝! –

0

這應該這樣做:

  • candidate_answers.test_question
  • candidate_answers
  • test_questions

    類測試<的ActiveRecord :: Base的 的has_many:結果 的has_many :問題 結束

    類結果<的ActiveRecord :: Base的 belongs_to的:測試 belongs_to的:候選人 結束

    類考生<的ActiveRecord :: Base的 的has_many:結果 的has_many:回答 結束

    類答< ActiveRecord的: :基地 belongs_to:問題 belongs_to:候選人 結束

    類問題<的ActiveRecord :: Base的 belongs_to的:測試 的has_many:回答 結束

它看起來像你打算重用一個以上問題的答案,這將是一個普遍糟糕的主意。在這種情況下,最好克隆一個答案。

+0

謝謝。很好的見解 –