I have a hash that I’m getting from using JSON.parse. I am inserting into db at that point. The data structure uses acts as tree so I can have a hash like this:
category_attributes
name: "Gardening"
items_attributes:
item 1: "backhoe"
item 2: "whellbarrel"
children_attributes
name: "seeds"
items_attributes
item 3: "various flower seeds"
item 4: "various tree seeds"
children_attributes
name: "agricultural seeds"
items_attributes
item 5: "corn"
item 6: "wheat"
For this hash, I’d like to return an array of all the items_attributes. I have seen this question Traversing a Hash Recursively in Ruby but it seems different. Is there a way to recursively search a hash and return all those elements? Ideally, empty items_attributes should come back with nothing rather than nil.
thx
You can do something like this: