Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:55:53+00:00 2026-06-04T00:55:53+00:00

This question relates to a RoR quiz app. I am looking to display the

  • 0

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 %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:55:55+00:00Added an answer on June 4, 2026 at 12:55 am

    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 %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question relates to this question which I asked earlier this week. The answer
This is kind of a two part question. But it both relates to the
This question relates to selected object pattern with knockout Thanks to the answer, I
This question relates to Francois Deschenes's answer to one of my previous questions. I
Well this question relates to MVVM pattern and i could good and fast answers
This question relates to this SO question and answer (rails-3-ssl-deprecation ) where its suggested
This question relates to an ASP.NET website, originally developed in VS 2005 and now
This question relates to those parts of the KenKen Latin Square puzzles which ask
Background: This question relates to versions of Delphi below 2009 (ie without Unicode support
James here. This question relates (sort of) to the one I posted a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.