使用每个循环通过哈希(1 vs More)

时间:2015-02-04 16:09:11

标签: ruby-on-rails hash each

我有一个正在解析并发送到我的函数的哈希。

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

如果我发送超过1个帖子,它会正确解析。并将每篇文章作为帖子发送

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>

1 个答案:

答案 0 :(得分:0)

添加

posts = Array.wrap(posts)

在你的行动的顶部,一切都应该工作。