使用 Each 遍历哈希(1 vs 更多)

Using Each to loop thru hashes (1 vs More)

我有一个散列正在被解析并发送到我的函数中。

我遇到的问题是,当我发送 1 post 时出错,因为它解析每个属性并将每个属性作为 post.

发送

如果我发送多于 1 个 post,它会正确解析。并将每个 post 作为 post

发送
def self.build_post(posts, type, user_id)
    posted_loads = 0
    posted_trucks = 0
    puts posts
    puts posts["#{type.downcase}"].count
    posts["#{type.downcase}"].each do |post|
      post = Hash[*post.flatten]
      puts post 
      record = build_record(post, user_id, type)
    ----- SOME MORE STUFF ----
end

当我只发送 1 个负载时

控制台显示:

  {"load"=>{"tracking_number"=>"493266", "origin"=>{"city"=>"BUENA PARK", "state"=>"CA", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "destination"=>{"city"=>"CORAL SPRINGS", "state"=>"FL", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "equipment"=>{"v"=>nil}, "loadsize"=>{"fullload"=>"true", "length"=>"53", "width"=>nil, "height"=>nil, "weight"=>"09"}, "load_count"=>"1", "stops"=>"0", "distance"=>"2600", "rate"=>"0.00", "comment"=>"DRY VAN"}}
  10
  {"tracking_number"=>"493266"}

当我发送 2 个或更多负载时(完美运行)

控制台显示:

  {"load"=>[{"tracking_number"=>"493266", "origin"=>{"city"=>"BUENA PARK", "state"=>"CA", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "destination"=>{"city"=>"CORAL SPRINGS", "state"=>"FL", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "equipment"=>{"v"=>nil}, "loadsize"=>{"fullload"=>"true", "length"=>"53", "width"=>nil, "height"=>nil, "weight"=>"09"}, "load_count"=>"1", "stops"=>"0", "distance"=>"2600", "rate"=>"0.00", "comment"=>"DRY VAN"}, {"tracking_number"=>"493266", "origin"=>{"city"=>"BUENA PARK", "state"=>"CA", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "destination"=>{"city"=>"CORAL SPRINGS", "state"=>"FL", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "equipment"=>{"v"=>nil}, "loadsize"=>{"fullload"=>"true", "length"=>"53", "width"=>nil, "height"=>nil, "weight"=>"09"}, "load_count"=>"1", "stops"=>"0", "distance"=>"2600", "rate"=>"0.00", "comment"=>"DRY VAN"}]}
  2
  {"tracking_number"=>"493266", "origin"=>{"city"=>"BUENA PARK", "state"=>"CA", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "destination"=>{"city"=>"CORAL SPRINGS", "state"=>"FL", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "equipment"=>{"v"=>nil}, "loadsize"=>{"fullload"=>"true", "length"=>"53", "width"=>nil, "height"=>nil, "weight"=>"09"}, "load_count"=>"1", "stops"=>"0", "distance"=>"2600", "rate"=>"0.00", "comment"=>"DRY VAN"}
  {"tracking_number"=>"493266", "origin"=>{"city"=>"BUENA PARK", "state"=>"CA", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "destination"=>{"city"=>"CORAL SPRINGS", "state"=>"FL", "postcode"=>nil, "county"=>nil, "country"=>nil, "latitude"=>"NaN", "longitude"=>"NaN", "date_start"=>{"year"=>"2009", "month"=>"06", "day"=>"04", "hour"=>"12", "minute"=>"00"}, "date_end"=>{"year"=>nil, "month"=>nil, "day"=>nil, "hour"=>nil, "minute"=>nil}}, "equipment"=>{"v"=>nil}, "loadsize"=>{"fullload"=>"true", "length"=>"53", "width"=>nil, "height"=>nil, "weight"=>"09"}, "load_count"=>"1", "stops"=>"0", "distance"=>"2600", "rate"=>"0.00", "comment"=>"DRY VAN"}

如果有多个,看起来像是将它放入一个哈希数组中。

这是我的控制器

def loads
    hash = Hash.from_xml(request.body.read)
    account = hash["PELoadPostings"]["PostingAccount"]
    post = hash["PELoadPostings"]["PostLoads"]
    post1 = hash["PELoadPostings"]["RemoveLoads"]
    if post
      message1 = PostEverywhereXml.post_loads(post, account)
    end
    if post1
      message1 = PostEverywhereXml.remove_loads(post1, account)
    end
    message = message1
    message ||= "Error: No loads in file"
    render :text => message
  end

正在发送示例负载数据

<load>
  <tracking-number>493266</tracking-number>
  <origin><city>BUENA PARK</city><state>CA</state><postcode></postcode><county></county><country></country>
    <latitude>NaN</latitude><longitude>NaN</longitude>
    <date-start><year>2009</year><month>06</month><day>04</day><hour>12</hour><minute>00</minute></date-start>
    <date-end><year></year><month></month><day></day><hour></hour><minute></minute></date-end>
  </origin>
  <destination>
    <city>CORAL SPRINGS</city><state>FL</state><postcode></postcode><county></county><country></country>
    <latitude>NaN</latitude><longitude>NaN</longitude>
    <date-start><year>2009</year><month>06</month><day>04</day><hour>12</hour><minute>00</minute></date-start>
    <date-end><year></year><month></month><day></day><hour></hour><minute></minute></date-end>
  </destination>
  <equipment><v/></equipment>
  <loadsize fullload="true">
    <length>53</length><width></width><height></height><weight>09</weight>
  </loadsize>
  <load-count>1</load-count><stops>0</stops><distance>2600</distance><rate>0.00</rate><comment>DRY VAN</comment>
</load>
<load>
  <tracking-number>493266</tracking-number>
  <origin><city>BUENA PARK</city><state>CA</state><postcode></postcode><county></county><country></country>
    <latitude>NaN</latitude><longitude>NaN</longitude>
    <date-start><year>2009</year><month>06</month><day>04</day><hour>12</hour><minute>00</minute></date-start>
    <date-end><year></year><month></month><day></day><hour></hour><minute></minute></date-end>
  </origin>
  <destination>
    <city>CORAL SPRINGS</city><state>FL</state><postcode></postcode><county></county><country></country>
    <latitude>NaN</latitude><longitude>NaN</longitude>
    <date-start><year>2009</year><month>06</month><day>04</day><hour>12</hour><minute>00</minute></date-start>
    <date-end><year></year><month></month><day></day><hour></hour><minute></minute></date-end>
  </destination>
  <equipment><v/></equipment>
  <loadsize fullload="true">
    <length>53</length><width></width><height></height><weight>09</weight>
  </loadsize>
  <load-count>1</load-count><stops>0</stops><distance>2600</distance><rate>0.00</rate><comment>DRY VAN</comment>
</load>

只需添加

posts = Array.wrap(posts)

在你的行动的顶部,一切都应该有效。