Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Ruby on Rails在subhash中搜索_Ruby On Rails_Search_Hash - Fatal编程技术网

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

谢谢你的回复,但我认为如果关键的“产品名称”是,比如说,几个子项,这个解决方案将不起作用。