views:

34

answers:

1

I have a hash of hashes like so:

Parameters: {"order"=>{"items_attributes"=>{"0"=>{"product_name"=>"FOOBAR"}}}}

Given that the depth and names of the keys may change, I need to be able to extract the value of 'product_name' (in this example "FOOBAR") with some sort of search or select method, but I cannot seem to figure it out.

An added complication is that Params is (I think) a HashWithIndifferentAccess

Thanks for your help.

A: 

Is this what you mean?

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
Crast
Thanks for your reply, but I do not think this solution will work if the key 'product_name' were, say, a couple of subhashes deeper.
doctororange