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 8961491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:54:20+00:00 2026-06-15T15:54:20+00:00

I have built two methods in exams_helper.rb to use in views: <% @topic_questions.each do

  • 0

I have built two methods in exams_helper.rb to use in views:

<% @topic_questions.each do |topic_question| %>
<tr>
  <td><%= topic_question.topic.name %></td>
  <td><%= correct_questions(@exam_result.exam_id, topic_question.topic_id) %></td>
  <td><%= number_to_percentage(ratio(@exam_result.exam_id, topic_question.topic_id), precision: 0) %></td>
</tr>
<% end %>

Method to calculate the number of correct question of topic:

  def correct_questions(exam_id, topic_id)
    total = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id).count
    correct = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id, correct: true).count
    correct.to_s + '/' + total.to_s
  end

Method to calculate percentage of correctness

  def ratio(exam_id, topic_id)
    total = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id).count
    correct = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id, correct: true).count
    ratio = (correct.to_f/total).round(2)*100
    if ratio.nan?
      ratio = 0
    else
      ratio
    end
  end

These code is repeated:

total = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id).count
correct = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id, correct: true).count

How can I write these method better?

  • 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-15T15:54:21+00:00Added an answer on June 15, 2026 at 3:54 pm

    In my opinion, these methods should rely in your Model as their purpose is to compute data from Database. Also, by writing these methods in the Model layer, you can avoid duplication in controllers, views or view helpers.

    View helpers should only be used for ‘view-logic’ methods, that do not make much sense outside of the view context.

    correct_questions and ratio seem to be closely related to the ExamResult object, we could imagine the following implementation:

    class ExamResult
      has_many :exam_questions
    
      def correct_questions_ratio(topic)
        ratio = (correct_questions(topic).to_f/total_questions(topic)).round(2)*100
    
        if ratio.nan?
          ratio = 0
        else
          ratio
        end
      end
    
      def total_questions(topic)
        #To avoid recomputing the result from db we memoize it for each topic.
        @total_questions ||= {}
        @total_questions[topic] ||= exam_questions.where(:topic_id => topic.id).count
      end
    
     def correct_questions(topic)
       #To avoid recomputing the result from db we memoize it for each topic.
       @correct_questions ||= {}
       @correct_questions[topic] ||= exam_questions.where(:topic_id => topic.id, :correct => true).count
     end
    end
    

    Memoization is a form of “caching” to avoid recomputing the same result many times. You can find many articles about it. Here is a good one : http://www.railway.at/articles/2008/09/20/a-guide-to-memoization/

    Finally, you would have the following code in your view. The helper is not really necessary anymore, but you can still write a helper method to construct the “correct/total” part, taking an ExamResult instance – @exam_result – as a parameter.

    <% @topic_questions.each do |topic_question| %>
      <tr>
        <td><%= topic_question.topic.name %></td>
        <td><%= @exam_result.correct_questions(topic_question.topic) %>/<%= @exam_result.total_questions(topic_question.topic)%></td>
        <td><%= number_to_percentage(@exam_result.correct_questions_ratio(topic_question.topic)), precision: 0) %></td>
      </tr>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Delphi 2010 built DLL with two methods: function Foo1(a, b: Integer):PChar; export;
I have built a custom hashmap using two arrays. One contains keys another values.
I have an ASP.NET MVC3 application that I have built and it has two
I have two app icons built-in (free and premium), is it possible to replace
I have two projects A and B both are built differently, but the jars
I have two webapps, that are built together and respectively provide a client and
If I have two byte[] arrays, is there a built-in function to compare them
Imagine that we have two static libraries built with different implementations of std::vector .
Suppose you have two static libraries A and B such that A references methods
I have the two functions shown below which I built for serialisation of long

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.