Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>View helpers should only be used for 'view-logic' methods, that do not make much sense outside of the view context.</p> <p><strong>correct_questions</strong> and <strong>ratio</strong> seem to be closely related to the ExamResult object, we could imagine the following implementation:</p> <pre><code>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 =&gt; 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 =&gt; topic.id, :correct =&gt; true).count end end </code></pre> <p>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 : <a href="http://www.railway.at/articles/2008/09/20/a-guide-to-memoization/" rel="nofollow">http://www.railway.at/articles/2008/09/20/a-guide-to-memoization/</a></p> <p>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.</p> <pre><code>&lt;% @topic_questions.each do |topic_question| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= topic_question.topic.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= @exam_result.correct_questions(topic_question.topic) %&gt;/&lt;%= @exam_result.total_questions(topic_question.topic)%&gt;&lt;/td&gt; &lt;td&gt;&lt;%= number_to_percentage(@exam_result.correct_questions_ratio(topic_question.topic)), precision: 0) %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload