2010-11-20 17 views
0

對於考試網站我的工作,我已經得到了以下型號:在Rails3中與查詢滯留:獲得一個結果頁面的檢查後

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 

的原因「嘗試」的模式是,每嘗試正在被保存,所以我們知道學生以後是否會改變他們的答案。 因此,在一個典型的設置:類別被選中,考試開始,問題彈出&每個回答任何問題的嘗試都會被自動記錄下來。 (所有這些運行..,但:) :)現在我想看看在下一個窗口中的結果:

目標:獲得一個列表的問題,與正確與否的結果(最後一次嘗試計數):

我開始:

e = Exam.last 
e.questions => this produces a list of questions, but how to see if these are correctly answered/not? 

正如你可以看到,即時通訊卡非常快:)任何想法?謝謝!

回答

1

這應該給你一個所有問題的列表和最後一次嘗試的答案。

<% @e.questions.each do |q| %> 
    <%= q.attempt.last.answer %> 
<% end %> 
+0

thx爲答案!但是:它會給我所有的問題。包括重複。我試着@ e.questions.group(:question_id).each,但那也不管用。想法? – 2010-11-20 16:05:30

+0

好吧,現在我得到了:e.questions.find(:all,:select =>「questions.id」,:group =>「questions.id」)。each {| q |放置「#{q.id} - #{q.attempts.last.correct}」} - 如果我有一個更清潔和更多的rails3解決方案,請讓我知道! – 2010-11-20 16:35:56

相關問題