Forum: Ruby on Rails Merge and group multiple hashes by key

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
João B. (Guest)
on 2017-01-25 01:36

i have a problem i my program that i can't solve. i'm recieving multiple
hashes and i what to merge them into only one hash and group them by
string, something like this:

{"Leadership"=>[3,2,5], "Motivation"=>[2,5], "Innovation"=> [2,4,4,2]}

the code that i have now is this:

    ...some code...

    self_cc = {}

    slf_cc.each do |cc|
      self_cc[cc.title] = cc.rate


    n1_cc = {}

    ...some code...

    n1_ccs.each do |cc|
      n1_cc[cc.title] = cc.rate

    ...some code...
    team_array = []
    ...some code...

    team_array.each do |team|

      team_user_ccs = mutual.where(team_id: team)

      team_user_ccs.each do |tucc|
        team_cc[tucc.title] = tucc.rate
        #{"Leadership"=>2, "Innovation"=>3, "Motivation"=>5}
    #new team with new "tucc"

    rates = {}

    keys = self_cc.keys
    keys.each do |k|
      rates[k] = [self_cc[k], n1_cc[k]]

for now i could only add to my hash the self_cc and the n1_cc and group
them, i wanted to add the team_cc but the "team_array.each do |team|"
part makes difficult because in the team_cc i can recieve more than one
hash of keys and values. The keys are the same so i wanted to group all
the values i received.

Can anyone help me please?

Thank you
This topic is locked and can not be replied to.