1

This question relates to a RoR quiz app. I am looking to display the results of the quiz, showing both the correct answer as well as the user's answer.

Therefore, in my view, I need to loop through each question and show the question answer, as well as the user's answer. However, I am struggling trying to figure out how to correctly query the user's answer.

To query the user_answer, I need the response_set where the user_id = current_user.id and I need to pass in the question_id as it loops through the each loop. However, I'm stuck on how to accomplish this.

What do I need to do to show the user_answer in the view alongside the correct_answer?

I have 3 models:

Response_set
belongs_to :user
has_many :responses

Reponse
belongs_to :response_set
belongs_to :question

Question
has_many :responses

Table fields:

create_table :response_sets do |t|
  t.integer :user_id

create_table :responses do |t|
  t.integer :response_set_id
  t.integer :question_id
  t.integer :user_answer

create_table :questions do |t|
  t.integer :correct_answer

Controller

@questions = Question.all
@user = current_user.id

View (just looping through the questions and not displaying the user's answer)

<% @questions.each do |question| %>
   <%= question.correct_answer %>
<% end %>
4
  • This is really ambiguous if you ask me Commented May 13, 2012 at 13:47
  • I think this might be a case for named scopes, but as a Rails noob I can't really offer a complete solution. Commented May 13, 2012 at 14:08
  • Do you have a current_user method or something? How do you know for which user to show the response? Commented May 13, 2012 at 14:17
  • Yes, I have a current user method. I'll add that to my question. In the controller: @user = current_user.id Commented May 13, 2012 at 14:26

2 Answers 2

2

My mentor ended up solving this one for me. Here is the solution that he presented:

"I think a better approach here would be get the responses from the response set and loop over that instead of looping over the questions."

Controller:

 @response_set = Response_set.where("user_id = ?", @user).includes({:responses => :question}).last

View:

 <% @response_set.responses.each do |response| %>
   <%= response.question.correct_answer %>
   <%= response.user_answer %>
 <% end %>
Sign up to request clarification or add additional context in comments.

Comments

0

Add user_id in responses table, this way you don't have to query response_set table. After this you can do something like this inside the loop

 question.responses.where(:user_id => current_user.id).first.user_answer

2 Comments

Sorry that I wasn't clear, but in my attempt to simplify my question, I left out that there is also a Test model. As there can be different tests, and a user can take multiple tests, it is necessary to keep the user_id in the response_set. Response_set belongs_to :test
As response belongs_to response_set and response_set belongs_to user. You can have user_id in both the tables(response and response_set), there is no harm in that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.