Basically I have an array of hashes like so :
[
{ :id => 20, :total => 1, :total2 => 0 },
{ :id => 21, :total => 1, :total2 => 0 },
{ :id => 22, :total => 2, :total2 => 0 },
{ :id => 23, :total => 1, :total2 => 0 },
{ :id => 20, :total => 1, :total2 => 0 },
{ :id => 21, :total => 1, :total2 => 0 },
{ :id => 22, :total => 1, :total2 => 1 },
{ :id => 23, :total => 1, :total2 => 0 }
]
I want the array to sum the last two hash columns like so, keeping the first (:id) as an identifier:
[
{ :id => 20, :total => 2, :total2 => 0 },
{ :id => 21, :total => 2, :total2 => 0 },
{ :id => 22, :total => 3, :total2 => 1 }
]
I have looked around and it seems that the .inject() method is used in this instance but I cannot really figure out the syntax/how to use this.
What I am looking for is to keep the first column (:id) as an ID field; if there is another hash with this ID, like in my example above, the two hashes should be added together.