【发布时间】:2015-05-10 13:33:51
【问题描述】:
我在一个数组和哈希兔子洞中,请原谅冗长的问题。
我正在尝试在视图中显示值。我通过将散列组合在一起并按值对它们进行分组来创建我的散列,因此我的数据如下所示:
[
{"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 等的输出。以上将是循环遍历哈希数组以及每个哈希循环遍历它的结果...
【问题讨论】:
-
你能给我们想要的输出吗?您已经分享了您的代码,但没有告诉我们它在哪里失败以及您在追求什么。
-
基本上我的问题包含我上面的伪代码所需的输出,例如,我希望能够循环遍历哈希数组中每个项目的“cbrs”:正如我所示
<% array.each do |a| %> <%= a.first_key %> <% a.cbrs.each do |c| <%=c.type%> <%end%>即输出将是447478xxxxxx cbr -
我刚刚编辑了我的问题,让您更好地了解我想要做什么
标签: ruby-on-rails arrays ruby hash