Question

I'm using morris.js to display charts in my application. In my controller I have the code:

controller

@users_count = User.group('DATE(created_at)').count.map do |d, c| {date: d, count: c} end

From this I get:

[{:date=>'2013-04-01', :count=>1}, {:date=>'2013-04-02', :count=>1}]

When I try to pass this to morris.js as a data option it doesn't work. I need to convert this to:

[{date: '2013-04-01', count: 1}, {date: '2013-04-02', count: 1}]

Then it works.

How can I do that?

Était-ce utile?

La solution

This has nothing to do with old vs new hash syntax. You cannot convert output to the new syntax. It will always use the old syntax. What morris expects is probably a JSON string. You can get it by using .to_json.

Use to_json on your result and pass that to morris.

@users_count = User.group('DATE(created_at)').count.map { |d, c| {date: d, count: c} }.to_json
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top