0

だから私はしばらくの間問題に苦しんでいて、どこにも行かないようです...

問題は、3レベルの深さでハッシュをループしているときに発生します。

したがって、私のコードは次のようになります。

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つの異なるバリアントがあるためです。1つは無限の「option_items」があり、もう1つは「option_item」が1つだけです。単一の「option_item」を返送する代わりに、この「option_item」をループしていて、この特定のフィールドが見つかりません。1つのオブジェクトだけがまだ配列として返されることがわかったときにそれを強制する方法についてのアイデアはありますか?

更新(2012年5月9日)

TypeError-配列インデックスとしてのシンボル

ハッシュから単一の「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"},
4

2 に答える 2

2

私の知る限り、ソースコードとサンプルデータを考えると、ループの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]

:itemslocation_option_itemは配列であるため、そこからシンボルにアクセスすることはできません。

取得itemsしたい場合は、次のようになります。

location_option_item[1][:items]
于 2012-09-05T16:44:59.587 に答える
2

クリスタの例は、option_itemが1つしかない場合は機能しますが、それ以上ある場合は機能しませんフラット化を追加して、次のように調整します。

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

その周りの[]は、それが配列であることを確認します。flattenは、それがすでに配列である場合は、[]の余分なレイヤーを削除することを確認します。例:

['a'].flatten
=> ["a"]
[['a','b','c',]].flatten
=> ["a", "b", "c"]
于 2012-09-06T04:21:08.843 に答える