Created
October 29, 2016 18:57
-
-
Save anonymous/1ac8faf81bbd6f9fac2cd3539270b701 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def total_weight(quiz) | |
weight = 0 | |
quiz.questions.each do |question| | |
weight += question.weight if question.present? | |
end | |
weight | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are working with ActiveRecord you can do
quiz.questions.sum(:weight)
If not you can use the
reduce
method (https://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-reduce):