I’ve got a ruby hash like this
[{user_id: 3, purchase: {amount: 2, type_id:3, name:"chocolate"},
{user_id: 4, purchase: {amount: 1, type_id:3, name: "chocolate"},
{user_id: 5, purchase: {amount: 10, type_id:4, name: "penny-candy"}]
I want to take the array and merge by the type_id, sum the amounts, connect the user to the amounts, so the end result would be
[{type_id: 3, name: "chocolate", total_amounts:3, user_purchases[{user_id:3, amount:2},user_id:4,amount:1}],
{type_id:4, name: "penny-candy", total_amounts: 10, [{user_id:5,amount:2}]}]
how would I go from one type of output to the other?
It’s a group_by problem. I’ll give you 2:
and leave the other 2 as an exercise