0

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 ...

3
  • can you give us the desired output? You've shared your code but failed to tell us where it failed vs. what you're after. Commented May 10, 2015 at 14:04
  • Essentially my question contains the desired output with my pseudo code above, for example I'd like to be able to loop through the 'cbrs' for each item in the array of hashes : as I show with <% array.each do |a| %> <%= a.first_key %> <% a.cbrs.each do |c| <%=c.type%> <%end%> Ie the output would be 447478xxxxxx cbr Commented May 10, 2015 at 14:05
  • I just edited my question to give you a better idea of what I'm trying to do Commented May 10, 2015 at 14:22

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Hey @Anthony, what if the order of the values can not be guaranteed? Say value.first[:cbrs] - what if that is not the first value? How would you write the line puts "CBRS: #{val_check(value.first[:cbrs])}" if you do not know which order the [:cbrs] appears in?
I can cbrs = value.find {|x| x[:cbrs] } then cbrs.values[0].first["name"] etc.
0

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

3 Comments

Well, you are using a mix of symbols and strings.
The first thing I'm accessing is the number, in my pseudo code in the question I use <%=a.first_key%> it is actually <%=a.keys.first%>. The first error I am getting at the minute is undefined method `cbrs' for #<Hash:0x007fe2956010b0> as you can't perform a loop as I suggest in my pseudo code.
Once inside the loop, using a.values will give you the values of the hash you are in..which would then allow you to check [:cbrs] and the like for the "0"(no items) or actual array items display.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.