The following query works as expected:
Purchase.all.group( :user_id ).sum( :price )
It returns an array of hashes:
[{ 1 : 234 }, ...
Is there a way to return an array of hashes with keys?
[{ id : 1, price : 234 }, ...
You can return an ActiveRecord::Relation with a single query.
Purchase.select("user_id as id, sum(price) as price").group("user_id")
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