2014-01-15 177 views
0

前言:這個問題是非常具體的。我一直在絞盡腦汁 - 我認爲我需要這位大師。軌道4 - 複雜的模型關係

概念:「測試者」需要回答多項選擇題(通常使用> 1的正確答案)。用戶選擇正確的答案(圖片),然後點擊「提交」

有點兒像這樣:

Which ones need baking?

我的問題:我不知道的最好辦法收集和儲存(並最終檢索)上的數據這種事情。具體來說:

  • 哪位用戶試圖回答這個問題?
  • 沒有用戶選擇哪些畫面?
  • 其中用戶選擇的圖片是正確/不正確?
  • 這個問題總體正確嗎? (例如,用戶只是部分正確的嗎?)

我的表結構

users 
    id 
=================================================== 
templates 
    id 
    prompt     #"Which one needs baking?" 
=================================================== 
choices     #e.g., the image 
    id 
    name 
    image 
=================================================== 
template_assignments #a join table between templates and choices 
    id 
    choice_id 
    template_id 
    correct    #boolean - Is this image the correct response? 
=================================================== 
responses    #this is where I am LOST 
    id 
    user_id 
    template_id 
    --Not sure what to do with this table, maybe something like: 
    response_1 
    response_1_correct #boolean 
    etc.... 
    overall_correct  #boolean 
    -- Or would I need some other type of join table? 

我的關係:

class Choice < ActiveRecord::Base 
    has_many :template_assignments 
    has_many :templates, :through => :template_assignments 
    end 

    class Template < ActiveRecord:Base 
    has_many :template_assignments, dependent: :destroy 
    has_many :choices, :through => :template_assignments 
    has_many :responses 
    accepts_nested_attributes_for :template_assignments, allow_destroy: true 
    end 

    class TemplateAssignment < ActiveRecord:Base 
    belongs_to :template 
    belongs_to :choice 
    end 

    class Response < ActiveRecord::Base 
    belongs_to :template 
    end 

如何任何建議定義這些模型之間的關係 很有幫助!

謝謝!

回答

0

這是我怎麼會設置項目。你需要的,如果你想存儲的選擇了幾個連接表

Templates  
* id 
* prompt 

Choices  
* id 
* name 
* image 

Responses 
* template_id 
* user_id 
* score 

TemplateChoices 
(there should be 4 rows for the question above) 
* template_id 
* choice_id 

TemplateSolutions 
(if there are three correct answers, then there should only be 3 rows) 
* template_id 
* choice_id 

用戶所做

ChoiceResponses 
* choice_id 
* response_id 

我會更新這個帖子,以幫助您對您有任何問題。 (順便說一句,我會用「Rails的連接表的命名慣例儘可能」)