I am trying to graph data that I am pulling from an ActiveRecord query in Ruby on Rails. The JavaScript charting library (amcharts) accepts data in the following form:
var chartData = [{
date: new Date(2012, 0, 1),
products: 227
}, {
date: new Date(2012, 0, 2),
products: 371
}]
How do I parse a date string in the format YYYY-MM-DD and create a JavaScript Date object from the query below? I’ve seen this tutorial but I’m having trouble figuring out how to write a function to correctly parse the json output.
create_table :products do |t|
t.date :date
end
<% a = Product.select("date(date) as date, count(id) as products").group("date(date)").to_a.to_json %>
Here is the answer that worked for me: