2012-09-05 142 views
0

所以我一直在挣扎的问题了一会儿,似乎是难以得逞的......当我通过散列循环,围绕第3级深度存在红宝石Rails的哈希问题

的问题。

所以我的代码看起来有点像:

request[:items][:location_item].each do |locaton_item| 
    pp location_item[:name] 
    location_item[:items][:sub_area_option_item].each do |sub_area_option_item| 
    pp sub_area_option_item[:name] 
    sub_area_option_item[:items][:option_item].each do |option_item| 
     option_item[:name] <- THIS IS WHERE MY CODE BREAKS 
    end 
    end 
end 

,这是失败的原因是循环的这一特定部分有2个不同的变种,一个地方有“option_items”和一个无限量哪里只有一个“option_item”。与其将单个“option_item”发送回来,它是循环通过此“option_item”,而不是找到此特定字段。当只有一个对象被发现仍然将其作为数组返回时如何强制它的任何想法?

UPDATE(05/092012)

类型错误 - 符号作为数组索引

如果我通过与一个条件语句过滤它从散列删除单个“option_item”或甚至该错误不会出现。

作为另一个更新,这是前一个例子,这里是代码:

request[:items][:house_option_item][:items][:location_option_item].each do |location_option_item| 
    pp location_option_item.try(:[], :location).try(:[], :name) 
    # pp location_option_item[:location][:name] 

    location_option_item[:items][:sub_area_option_item].each do |sub_area_option_item| 
    pp "--" + sub_area_option_item[:sub_area][:name] + "--" + sub_area_option_item[:sub_area][:id] 
    count = 0 

    # pp sub_area_option_item[:items].length 
    # pp "+++++++++++++++++" 

    # if sub_area_option_item[:sub_area][:name] != "Conservatory" 
     [sub_area_option_item[:items][:option_item]].each do |option_item_key_info| 

     pp "----" 
     pp option_item_key_info[:usage_name] 

     end 
    # end 

    end 
end 

实施例的数据(简称):

{:"@xmlns:i"=>"http://www.w3.org/2001/XMLSchema-instance", 
:"@xmlns:a"=>"http://schemas.datacontract.org/2004/07/RXTypes", 
:items=> 
    {:house_option_item=> 
    {:design=> 
     {:house_style=>nil, 
     :sales_name=>nil, 
     :sq_ft=>"0", 
     :"@xmlns:b"=>"http://schemas.datacontract.org/2004/07/", 
     :arch_ref=>"D3H102.1", 
     :marketing_name=>"Warwick", 
     :num_beds=>"0"}, 
    :items=> 
     {:location_option_item=> 
     [{:location=>{:sort_order=>"0", :name=>"Eat", :id=>"1"}, 
      :items=> 
      {:sub_area_option_item=> 
      [{:sub_area=> 
       {:location_id=>"7", 
       :sort_order=>"0", 
       :name=>"Conservatory", 
       :id=>"31"}, 
       :items=> 
       {:option_item=> 
        {:lead_days=>"0", 
        :collection_code=>"NEWHE01", 
        :usage_name=>"Conservatory", 
        :max_qty=>"1", 
        :is_a_bundle=>false, 
        :only_order_bundle=>false, 
        :sub_area=>"Conservatory", 
        :key_info=> 
        {:specification=> 
         "PVC-U 'Edwardian' style Conservatory in white with high performance toughened glass double glazed windows and 600mm (approx) dwarf base wall internally and externally. Conservatory to include fan and light, electric heater, double power point and ceramc floor tiling selected from tile choice . Size of Conservatory to be as follows: 3.2 x 3.1 metre (Availabil 
ity will be subject to ground levels/conditions)", 
        :description=>"Conservatory", 
        :image_url=>"/2012/8/10/334_web.jpg", 
        :item_id=>"3", 
        :thumbnail_url=>"/2012/8/10/334_thumb.jpg"}, 
        :build_stage_id=>"0", 
        :location=>"Building", 
        :option_flag=>"67", 
        :bundle_items=>nil, 
        :price=>"8950", 
        :choice_items=>nil, 
        :spec_level=>"85"}}}, 
       {:sub_area=> 
       {:location_id=>"7", 
       :sort_order=>"0", 
       :name=>"Doors", 
       :id=>"32"}, 
       :items=> 
       {:option_item=> 
        [{:lead_days=>"0", 
        :collection_code=>"NEWHE01", 
        :usage_name=>"Front Door", 
        :max_qty=>"0", 
        :is_a_bundle=>false, 
        :only_order_bundle=>false, 
        :sub_area=>"Doors", 
        :key_info=> 
        {:specification=> 
         "GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige 
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.", 
         :description=>"GRP Front door", 
         :image_url=>nil, 
         :item_id=>"4", 
         :thumbnail_url=>nil}, 
        :build_stage_id=>"0", 
        :location=>"Building", 
        :option_flag=>"88", 
        :bundle_items=>nil, 
        :price=>"0", 
        :choice_items=> 
        {:option_item_key_info=> 
         [{:specification=>nil, 
         :description=>"NG42 Diamond", 
         :image_url=>"/2012/8/21/485_web.jpg", 
         :item_id=>"211", 
         :thumbnail_url=>"/2012/8/21/485_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG21 Square", 
         :image_url=>"/2012/8/21/486_web.jpg", 
         :item_id=>"212", 
         :thumbnail_url=>"/2012/8/21/486_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG51 Square", 
         :image_url=>"/2012/8/21/487_web.jpg", 
         :item_id=>"213", 
         :thumbnail_url=>"/2012/8/21/487_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG42 Rectangle", 
         :image_url=>"/2012/8/21/488_web.jpg", 
         :item_id=>"214", 
         :thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]}, 
        :spec_level=>"83"}, 
        {:lead_days=>"0", 
        :collection_code=>"NEWHE01", 
        :usage_name=>"Front Door", 
        :max_qty=>"0", 
        :is_a_bundle=>false, 
        :only_order_bundle=>false, 
        :sub_area=>"Doors", 
        :key_info=> 
        {:specification=> 
         "GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige 
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.", 
         :description=>"GRP Front door", 
         :image_url=>nil, 
         :item_id=>"4", 
         :thumbnail_url=>nil}, 
        :build_stage_id=>"0", 
        :location=>"Building", 
        :option_flag=>"88", 
        :bundle_items=>nil, 
        :price=>"0", 
        :choice_items=> 
        {:option_item_key_info=> 
         [{:specification=>nil, 
         :description=>"NG42 Diamond", 
         :image_url=>"/2012/8/21/485_web.jpg", 
         :item_id=>"211", 
         :thumbnail_url=>"/2012/8/21/485_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG21 Square", 
         :image_url=>"/2012/8/21/486_web.jpg", 
         :item_id=>"212", 
         :thumbnail_url=>"/2012/8/21/486_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG51 Square", 
         :image_url=>"/2012/8/21/487_web.jpg", 
         :item_id=>"213", 
         :thumbnail_url=>"/2012/8/21/487_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG42 Rectangle", 
         :image_url=>"/2012/8/21/488_web.jpg", 
         :item_id=>"214", 
         :thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]}, 
        :spec_level=>"83"}, 
        {:lead_days=>"0", 
        :collection_code=>"NEWHE01", 
        :usage_name=>"Front Door", 
        :max_qty=>"0", 
        :is_a_bundle=>false, 
        :only_order_bundle=>false, 
        :sub_area=>"Doors", 
        :key_info=> 
        {:specification=> 
         "GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige 
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.", 
         :description=>"GRP Front door", 
         :image_url=>nil, 
         :item_id=>"4", 
         :thumbnail_url=>nil}, 
        :build_stage_id=>"0", 
        :location=>"Building", 
        :option_flag=>"88", 
        :bundle_items=>nil, 
        :price=>"0", 
        :choice_items=> 
        {:option_item_key_info=> 
         [{:specification=>nil, 
         :description=>"NG42 Diamond", 
         :image_url=>"/2012/8/21/485_web.jpg", 
         :item_id=>"211", 
         :thumbnail_url=>"/2012/8/21/485_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG21 Square", 
         :image_url=>"/2012/8/21/486_web.jpg", 
         :item_id=>"212", 
         :thumbnail_url=>"/2012/8/21/486_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG51 Square", 
         :image_url=>"/2012/8/21/487_web.jpg", 
         :item_id=>"213", 
         :thumbnail_url=>"/2012/8/21/487_thumb.jpg"}, 
         {:specification=>nil, 
         :description=>"NG42 Rectangle", 
         :image_url=>"/2012/8/21/488_web.jpg", 
         :item_id=>"214", 
         :thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]}, 
        :spec_level=>"83"}, 
+1

什么是错误讯息? – robbrit

+0

TypeError - 代码第一行中的数组索引 – Anthony

+1

,block参数为'locaton_item',但在第二行和第三行为'location_item' – zeacuss

回答

2

克丽斯塔的例子,如果有只有一个option_item,但如果有更多的将工作。我想补充扁平化,把它调整:围绕它确保它是一个数组

[sub_area_option_item[:items][:option_item]].flatten.each do |option_item| 
    option_item[:name] 
end 

的[]。扁平化可以确保,如果它已经是一个数组,您删除的[例如]额外的层:

['a'].flatten 
=> ["a"] 
[['a','b','c',]].flatten 
=> ["a", "b", "c"] 
+1

辉煌的伴侣......似乎按预期完美运作。你说对于有多个块返回的情况是正确的。谢谢! – Anthony

+1

@Taryn - 谢谢!我其实并没有意识到“扁平化” - 这是一件好事,要知道:) – Krista

2

据我所知,第二部分的循环应该是问题 - 给定源代码和示例数据

01: request[:items][:house_option_item][:items][:location_option_item].each do |location_option_item| 

02: location_option_item[:items][:sub_area_option_item].each do |sub_area_option_item| 
03:  count = 0 
04:  # if sub_area_option_item[:sub_area][:name] != "Conservatory" 
05:  [sub_area_option_item[:items][:option_item]].each do |option_item_key_info| 
06:   pp option_item_key_info[:usage_name] 
07:  end 
08:  # end 

09: end 
10: end 

请注意,行01最终会访问哪些location_option_item您的样品中上述的是:

:location_option_item=> 
    [{:location=>{:sort_order=>"0", :name=>"Eat", :id=>"1"}, 
     :items=> 
     {:sub_area_option_item=> 
     [{:sub_area=> 
      {:location_id=>"7", 
      :sort_order=>"0", 
      :name=>"Conservatory", 
      :id=>"31"}, 
      :items=> 
      {:option_item=> 
       {:lead_days=>"0", 
       :collection_code=>"NEWHE01", 
       ........ etc ....... 

注意location_option_item是关键,其具有用于值的数组。

因此,我的猜测是,这条线是问题(02):location_option_item[:items]

由于location_option_item是一个数组,你不能从它访问:items符号。

如果你想获得items这将是更多的东西一样:

location_option_item[1][:items] 
+0

谢谢克里斯塔,但问题是可能有多个“option_item”的情况。 – Anthony