diff --git a/app/components/admin/poll/results/question_component.html.erb b/app/components/admin/poll/results/question_component.html.erb index aac83af2f..8a3d6633a 100644 --- a/app/components/admin/poll/results/question_component.html.erb +++ b/app/components/admin/poll/results/question_component.html.erb @@ -11,7 +11,7 @@ <% question.question_options.each_with_index do |option, i| %> <%= option.title %> - <%= by_answer[option.title].present? ? by_answer[option.title].sum(&:amount) : 0 %> + <%= votes_for(option) %> <% end %> diff --git a/app/components/admin/poll/results/question_component.rb b/app/components/admin/poll/results/question_component.rb index c12d9d344..8836a6b91 100644 --- a/app/components/admin/poll/results/question_component.rb +++ b/app/components/admin/poll/results/question_component.rb @@ -6,7 +6,14 @@ class Admin::Poll::Results::QuestionComponent < ApplicationComponent @partial_results = partial_results end - def by_answer - @by_answer ||= partial_results.where(question: question).group_by(&:answer) + def votes_for(option) + grouped = by_answer[option.title] || [] + grouped.sum(&:amount) end + + private + + def by_answer + @by_answer ||= partial_results.where(question: question).group_by(&:answer) + end end