sql - Is there a way to use group to select the id's of the grouped records in Rails? -
purchase.all.group( :user_id ).sum( :price )
this returns
[{ 1 : 234 }, { 2 : 345 }, ...
is possible pull purchase
ids?
for example, suppose first group returned:
{ user_id : 1, price : 234, ids : [3,6,9] }
using postgresql, can use array_agg
:
purchase.select('user_id, sum(price) price, array_agg(id) ids').group('user_id')
Comments
Post a Comment