您可以进行后期处理。您将不得不为子哈希中的“id”键提供单独的名称,但如果您这样做,这样的事情应该可以工作......到目前为止,无论如何都很好:
给定一个这样的存储哈希:
x => #<Company id: 16, name: "JRapid", markets: {"markets"=>"[{:market_id=>12, :market_name=>\"enterprise software\", :parents=>[{:parent_id=>12, :name=>\"enterprise software\", :grandparents=>{:parent_id=>12, :name=>\"enterprise software\"}}]}, {:market_id=>38, :market_name=>\"cloud computing\", :parents=>[{:parent_id=>38, :name=>\"cloud computing\", :grandparents=>{:parent_id=>38, :name=>\"cloud computing\"}}]}, {:market_id=>409, :market_name=>\"development platforms\", :parents=>[{:parent_id=>409, :name=>\"development platforms\", :grandparents=>{:parent_id=>409, :name=>\"development platforms\"}}]}, {:market_id=>1132, :market_name=>\"developer tools\", :parents=>[{}]}]"}, locations: {"locations"=>"[{:location_id=>1624, :location_name=>\"california\", :parents=>[{}]}, {:location_id=>1703, :location_name=>\"sunnyvale\", :parents=>[{}]}]"}, follower_count: 8, high_concept: "Rapid development Java cloud platform", product_desc: "JRapid is a Platform as a Service and is the fastes...", urls: {"blog_url"=>"http://www.jrapid.com/blog", "logo_url"=>"https://angel.co/images/icons/startup-nopic.png", "thumb_url"=>"https://angel.co/images/icons/startup-nopic.png", "company_url"=>"http://www.jrapid.com", "twitter_url"=>"http://www.twitter.com/JRapid", "angellist_url"=>"https://angel.co/jrapid"}, status: nil, created_at_or_updated_at: {"created_at"=>"2010-07-21T18:48:32Z", "updated_at"=>"2011-05-07T20:00:37Z"}, screenshots: {"screenshots"=>"[[nil]]"}, created_at: "2012-08-07 05:37:54", updated_at: "2012-08-07 05:37:54">
你可以这样做:
x = x.locations
x = x['locations']
x = eval(x)
x[0][:id]
#=> 1624
警告:在给定的字符串上运行 eval() 几乎不需要任何东西。因此,这可能不是“生产模式”解决方案。事实上并非如此。但在您学习如何使用一些真正的 Document-DB 解决方案之前,它会暂时起作用。再次:警告!运行 eval 可能很危险!
(如果这对您有帮助,请单说---我因提出太多问题而被禁止参加 SO,并且需要更多代表点才能再次提出问题)