score:2

Accepted answer

Let's make your method simpler while we're fixing it. If you ever find yourself with code like this:

foo = []
bar.each{ … foo << jim }

…then instead you should be using this:

foo = bar.map{ … jim }

You have Date.UTC in your Ruby code, which requires Ruby to run this code while building the array. Instead of creating an array of arrays, we'll use Enumerable#map to convert your codes_by_user into an array of strings that are the JS code that you want:

def byusers_chart_series(name, byusers)
  codes_by_user = byusers.where(:hpmuser => "#{name}").group("date(hpmcreated)").select("date(hpmcreated) as dater, count(globalcode) as codes")
  codes_by_user.map do |record|
    parts = %w[year month day].map{ |s| record.dater.send(s) }
    "[Date.UTC(#{parts.join(',')}),#{record.codes}]"
  end
end

And then in your view:

data: [ <%= byusers_chart_series(name, byusers).join(", ") %> ],

This will produce output like the following in your HTML:

data: [ [Date.UTC(2011,10,7),42], [Date.UTC(2012,4,3),17] ],

Related Articles