score:1

Well, for starters, you can simplify your code for the same basic approach with the following:

when 'likely1-5'
  (1..5).each do |k|
    v = stats[k.to_s] || 0
    report_collection.push(v.to_i)
  end

score:1

Let's see if this works.

question_id = xxx
stats = ResponseItem.select('score, count(id) as count').
                     where(question_id: question_id).
                     joins('RIGHT OUTER JOIN (SELECT "1" AS score UNION SELECT "2" UNION SELECT "3" UNION SELECT "4" UNION SELECT "5") as scores on score = answer_text').
                     group('score')
scale = stats.reduce({}) do |memo, answer_count|
  memo.merge(answer_count.score.to_i => answer_count.count)
end

Related Articles