3👍
✅
I’m not sure how to put this in the correct format of having an array
of “book counts per month” and an “array of labels” that have the
month names.
By looking at group_by_month
, it is clear that you are using groupdate
gem. Lets use its format
option to return just month names as keys
like below
@books_count = Book.group_by_month(:created_at, format: "%b").count
which would return the results like this
{"Jul" =>12, "Aug" =>10, "Sep" =>9, "Oct" =>4}
Now by doing @books_count.keys
and @books_count.values
should give the below
@books_count.keys
#=> ["Jul", "Aug", "Sep", "Oct"]
@books_count.values
#=> [12, 10, 9, 4]
So now you can finally feed them to the respected labels
and data
as below
labels: <%= @books_count.keys.to_json.html_safe %>
data: <%= @books_count.values.to_json.html_safe %>
Source:stackexchange.com