我在一个阵列&哈希兔洞,请原谅冗长的问题。
我正在尝试在视图中显示值。我通过将哈希值组合在一起并使用值对它们进行分组来创建哈希值,因此我的数据如下所示:
[
{"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"}
]
}
]
在我看来,我正试图做这类事情
<% 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%>
但是我不知道如何访问数组中哈希内数组内哈希中包含的值! (想想我做对了。)
编辑:我所追求的内容很简单 - 我只是想为数组中的每个项目做这样的事情:
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
希望有帮助,我只是在没有html等的情况下写出输出。上面的内容是循环哈希数组和循环遍历它的每个哈希......
答案 0 :(得分:1)
这可能会被重构4到5次,但这应该会让你得到你所追求的:
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
输出:
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
答案 1 :(得分:0)
你的第一个错误在哪里抛出?看起来你需要访问c [“from_number”]和c [“to_number”]之类的东西。