I am building a rails application, and I need to create some charts.
I am running this query to retrieve the answers from the user:
quiz = Quiz.select("answer1").where(completed: true).pluck(:answer1)
And the query returns for me this: [1, 2, 1, 1, 1]
I want to count the values and group them like this: { 1 => 4, 2 => 1 }
I have tried to use group by and count but it is not working, I could do this manually but I wanted to use just SQL to achieve this.
I remember to use group by and count using sql, but I am not sure how to do this using rails.
You can group('answer1')
as described here
Quiz.where(completed: true).group('answer1').count
Hope it helped!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With