Ruby on rails 使用Ruby on Rails在subhash中搜索
我有一个散列,如下所示:Ruby on rails 使用Ruby on Rails在subhash中搜索,ruby-on-rails,search,hash,Ruby On Rails,Search,Hash,我有一个散列,如下所示: Parameters: {"order"=>{"items_attributes"=>{"0"=>{"product_name"=>"FOOBAR"}}}} 考虑到键的深度和名称可能会发生变化,我需要能够通过某种搜索或选择方法提取“product_name”(在本例中为“FOOBAR”)的值,但我似乎无法找到它 另一个复杂的情况是,Params(我认为)是一个hashwithindifferencetaccess 谢谢你的帮助。这就是你的意思吗
Parameters: {"order"=>{"items_attributes"=>{"0"=>{"product_name"=>"FOOBAR"}}}}
考虑到键的深度和名称可能会发生变化,我需要能够通过某种搜索或选择方法提取“product_name”(在本例中为“FOOBAR”)的值,但我似乎无法找到它
另一个复杂的情况是,Params(我认为)是一个hashwithindifferencetaccess
谢谢你的帮助。这就是你的意思吗
if params.has_key?("order") and params["order"].has_key?("items_attributes") then
o = params["order"]["items_attributes"]
o.each do |k, v|
# k is the key of this inner hash, ie "0" in your example
if v.has_key?("product_name") then
# Obviously you'll want to stuff this in an array or something, not print it
print v["product_name"]
end
end
end
谢谢你的回复,但我认为如果关键的“产品名称”是,比如说,几个子项,这个解决方案将不起作用。