对于我正在研究的考试网站,我有以下模型:
class Exam < ActiveRecord::Base
belongs_to :user
#has_many :categories
has_many :questions, :through => :attempts
has_many :attempts
end
class Attempt < ActiveRecord::Base
belongs_to :exam
belongs_to :question
belongs_to :answer
belongs_to :user
end
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :answers, :dependent => :destroy
has_many :exams
has_many :attempts
end
class Category < ActiveRecord::Base
acts_as_nested_set
has_many :questions, :dependent => :destroy
has_many :subcategories, :class_name => 'Category', :foreign_key => 'parent_id'
belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id'
end
'尝试'模式的原因是每次尝试都被保存,所以我们知道学生以后是否会改变他们的答案。 因此,在典型的设置中:选择了类别,考试开始,问题弹出&amp;每次尝试回答任何问题都会自动记录下来。 (所有这些运行..,但是:)现在我想在下一个窗口中看到结果:
目标:获取包含问题的列表,结果是否正确(最后一次尝试计数):
我的开始:
e = Exam.last
e.questions => this produces a list of questions, but how to see if these are correctly answered/not?
正如你所看到的,即时通讯速度非常快:)任何想法? THX!
答案 0 :(得分:1)
这应该会列出所有问题和最后一次尝试的答案。
<% @e.questions.each do |q| %>
<%= q.attempt.last.answer %>
<% end %>