前言:这个问题非常具体。我一直在绞尽脑汁 - 我想我需要一个古茹。
概念:“考生”需要回答多项选择题(通常带有> 1个正确答案)。用户选择正确的答案(图片),然后单击“提交”。
Sorta喜欢这个:
我的问题: 我不知道收集和存储的最佳方法(最后检索 >)关于这类事情的数据。具体做法是:
我的表格结构:
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 :(得分: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连接表命名约定')