'NoMethodError in TransactionsController#index

I want to add the sum of all the prices of a transaction of a current user,

I have this in my transaction controller

@group_transaction_id = @transactions.group_by(&:user_to_id).sum(&:price)

and this in my view

<%= @group_transaction_id[current_user.id] %>

but it throws NoMethodError in TransactionsController#index

enter image description here

whenever I remove .sum(&:price), it shows this on the browser

enter image description here

I want to calculate the total price of what is showing.



Solution 1:[1]

Since group_by converts the records into hash, you can try something like this to calculate the sum of price:

@group_transaction_id = @transactions.group_by(&:user_to_id).map{|k,v| v.last[:price]}.sum

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 timtim