Purchase.all.group( :user_id ).sum( :price )
This returns
[{ 1 : 234 }, { 2 : 345 }, ...
Is it possible to pull the Purchase
ids?
For example, suppose the first group returned:
{ user_id : 1, price : 234, ids : [3,6,9] }
Using postgresql, you can use array_agg
:
Purchase.select('user_id, sum(price) as price, array_agg(id) as ids').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