LpLrich
LpLrich

Reputation: 2533

Accessing values in array of hashes where the values are arrays of hashes

I'm in an array & hash rabbit hole, please excuse verbose question.

I'm trying to display values in a view. I created my hash by combining hashes together and grouping them by a value, so my data looks like this:

[
  {"447478xxxxxx"=>[
                     {:cbrs=>[
                               {"telephone_number"=>"447478xxxxxx", "type"=>"cbr"}
                             ]
                     }, 
                     {:pupil_calls=>"0"}, 
                     {:returned_calls=>"0"}
                    ]
  }, 
  {"447440xxxxxx"=>[
                    {:cbrs=>[
                              {"telephone_number"=>"447440xxxxxx", "type"=>"cbr"}
                            ]
                    }, 
                    {:pupil_calls=>"0"}, 
                    {:returned_calls=>[
                                       {"from_number"=>"447952xxxxxx", "to_number"=>"447440xxxxxx", "type"=>"call", "duration"=>50, "direction"=>"outbound"}, 
                                       {"from_number"=>"447952xxxxxx", "to_number"=>"447440xxxxxx", "type"=>"call", "duration"=>nil, "direction"=>"outbound"}
                                       ]
                    }
                   ] 
  },
  {"447588xxxxxx"=>[
                      {:cbrs=>"0"}, 
                      {:pupil_calls=>[
                                       {"from_number"=>"447588xxxxxx", "to_number"=>"441483xxxxxx", "type"=>"call", "duration"=>5, "direction"=>"inbound"}
                                      ]
                      }, 
                      {:returned_calls=>"0"}
                    ]
  }
]

In my view I am trying to do this sort of thing

<% array.each do |a| %>

<%= a.first_key %> #this is the number at the start each group eg 447478xxxxxx`

<% a.cbrs.each do |c| %>

   <%=c.type%> #for example, this is just limited sample of the scope of the data

<%end%>

<% a.pupil_calls.each do |c| %>

   <%=c.from_number%> - <%=c.to_number%> 

<%end%>

<% a.returned_calls.each do |c| %>

   <%=c.duration%>

<%end%>

<%end%>

But I don't know how to go about accessing the values contained within the hashes within the arrays within the hashes within the array! (Think I got that right.)

Edit: What I'm after is simple enough - I just want to be able to do something like this for each item in array:

 Tel: 447478xxxxxx 
 CBRS: 1 
 Calls: 0
 Returned: 0

 Tel: 447440xxxxxx
 CBRS: 1
 Calls: 0
 Returned Calls: 2 
 Call first returned about 5 minutes after CBR #This would be using created_at dates for example, there is a lot of info I didn't include in my sample data.   
 Returned Call 1: recording link
 Returned Call 2: recording link

Hope that helps, I just wrote out the output without html etc. The above would be as a result of looping through array of hashes and for each hash looping through it ...

Upvotes: 0

Views: 62

Answers (2)

Anthony
Anthony

Reputation: 15967

This could be refactored another 4 or 5 times but this should get you what you're after:

def val_check(num)
  if num.is_a? Array
    num.size
  else
    num
  end
end


phone_numbers.each do |number|
  number.each do |key, value|
    puts "Tel: #{key}"
    puts "CBRS: #{val_check(value.first[:cbrs])}"
    puts "Calls: #{val_check(value[1][:pupil_calls])}"
    puts "Returned Calls: #{val_check(value[2][:returned_calls])}"
  end
end

Output:

Tel: 447478xxxxxx
CBRS: 1
Calls: 0
Returned Calls: 0
Tel: 447440xxxxxx
CBRS: 1
Calls: 0
Returned Calls: 2
Tel: 447588xxxxxx
CBRS: 0
Calls: 1
Returned Calls: 0

Upvotes: 1

snkashis
snkashis

Reputation: 2991

Where is your first error being thrown? Looks like you need to access things like c["from_number"] and c["to_number"].

Upvotes: 0

Related Questions