Get hash value Rails / Chart JS / Google Chart

๐Ÿ‘:1

count applies the SQL COUNT to your query, resulting in something like:

SELECT COUNT(*)
AS count_all, "polls"."nom"
AS polls_nom
FROM "polls"
WHERE "polls"."question_id" = $1
GROUP BY "polls"."nom"

And you get a Hash object, so you can access their values iterating like usual in a Hash:

@pollsQuestion4.each do |key, value|
  ...
end

If otherwise you want only the values, then you can use Hash#values:

@pollsQuestion4.values

And then you get an array.

๐Ÿ‘:1

You can return the values of a hash as an array using hash.values.
Iโ€™m not familiar with chart.js particularly, but based on this example it looks like you just need this data as a Javascript array; so in your view Iโ€™d expect something like

data: <%= hash.values.to_json %>

in a js.erb file to work, rendered using the formats: :js option.

Leave a comment