2014-01-15 39 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的连接表的命名惯例尽可能”)