0

Background

I have a complicated dynamic survey engine in my app, as a result I store answers in rows in a response table (instead of in a big flat survey table). One of the type of questions I'm now generating reports on all people who answer questions that have a 1-5 scales (1 - very unlikely ... 5 very likely). I generate my reports out of a reports_controller and send JSON to any report pages using them.

I'm generating reports to show these in a highcharts column format. The problem I originally ran into was wanting to show an accurate count even if nobody answers that response (i.e. send a 0 count in JSON).

I spent a lot of time trying to think of an elegant way to do this, and ended up creating a default empty hash of zeros, then merging it with my answers if the answer text matches.

My question...

Is there a better way to do this? It performs fine (on a small scale right now), but doesn't seem very readable and since the answer_text is string in the results "1" => 3 instead of 1 => 3, I have to use to_s to match the keys.

ReportController

question = params["question"]
format = params["report_type"]

stats = ResponseItem.joins("join table1 on table1.id = table2.table1id "\
      "join table2 on table2.id = table1.table3id "\
      "join table4 on table4.table3id = table2.id") \
      .where("table2.question_text = ? and client_id IN (?)", question, @clients.map(&:id)) \
      .select("count(*), answer_text") \
      .group("answer_text").order("answer_text").count()

case format

  when 'likely1-5'
    scale = {1 => 0, 2 => 0, 3  => 0, 4 => 0, 5 => 0}
    scale.each do |k, v|
      if stats.key?(k.to_s)
        scale[k] = stats[k.to_s].to_s
      end
      report_collection.push scale[k].to_i
    end

2 Answers 2

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
1
  • That certainly cleans it up a lot and gets rid of the extraneous hash. I was playing around with a 1 through 5 loop and can't remember why I ditched it. I'll check this option out. Thanks. Nov 11, 2013 at 16:32
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
2
  • I hadn't considered doing outer joins for some reason, seems like a good idea. Although all the other tables (the ones I obfuscated in my question above) may rule this out (need to restrict results by a complex set of which clients they are assigned to - when a user is logged in). Thanks. Nov 11, 2013 at 16:34
  • There's no stopping you from doing all of the inner joins before the outer join to give you your where, but the code would be much harder to read. Personally I'd do it in 2 queries (the first being the hand-waved xxx), but you'd want to check performance/memory usage with real data before deciding.
    – jimworm
    Nov 12, 2013 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.