Ruby on rails 使用每个循环通过散列(1对更多)

Ruby on rails 使用每个循环通过散列(1对更多),ruby-on-rails,hash,each,Ruby On Rails,Hash,Each,我有一个散列正在被解析并发送到我的函数中 我遇到的问题是,当我发送1个post时,它会出错,因为它会解析每个属性并将每个属性作为post发送 如果我发送的帖子超过1篇,它就会正确解析。并将每个帖子作为帖子发送 def self.build_post(posts, type, user_id) posted_loads = 0 posted_trucks = 0 puts posts puts posts["#{type.downcase}"].count

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

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

如果我发送的帖子超过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>
加上

posts = Array.wrap(posts)
在你行动的顶端,一切都应该起作用