Maurice Kroon
Maurice Kroon

Reputation: 959

Stuck with query in Rails3: Getting a results page after an examination

For an exam site i'm working on, ive got the following models:

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

The reason for the 'attempts' model is that every attempt is being saved, so we know if the student changes their answer later on. So in a typical setup: Category is selected, Exam starts, Questions pop up & every attempt in answering any questions is automatically recorded. (all this runs.., BUT:) Now I would like to see the results in a next window:

Goal: Get a list with questions, with the result of being correct or not (the last attempt counts):

My start:

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

As you can see, im stuck pretty fast :) Any ideas? thx!

Upvotes: 0

Views: 139

Answers (1)

Patrick
Patrick

Reputation: 8063

This should give you a list of all questions and the answer to the last attempt.

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

Upvotes: 1

Related Questions