本文介绍了如何迭代所有记录并将数据库值传递给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,Que和Opts。

I have two tables, "Que" and "Opts".

我想遍历Que中的所有记录,并将它们添加到变量 rikt_nr start_nr end_nr ,因为它们将位于URL结尾,如下所示:

I want to iterate through all the records in Que and add them to the variables rikt_nr, start_nr and end_nr, because they will go on the end of a URL, which will look like:

api.url.number=8-00001


$ b b

如何让它通过Que迭代,并传递 rikt_nr start_nr

Que表包含以下字段:

The Que table has these fields:

create_table "ques", force: true do |t|
  t.integer  "rikt_nr"
  t.integer  "start_nr"
  t.integer  "end_nr"
  t.datetime "created_at"
  t.datetime "updated_at"
end

且Opts有以下字段:

and Opts has these fields:

create_table "opts", force: true do |t|
  t.string   "numbers"
  t.string   "operator"
  t.datetime "created_at"
  t.datetime "updated_at"
end

这是代码:

Que.all.each do | que |

  url = "http://api.url.number=" 

  que.rikt_nr = rikt_nr 
  que.start_nr = start_nr  
  que.end_nr = end_nr  
  st_en = start_nr..end_nr 

  st_en.each do |nr| 
    full_url = "#{url}" + rikt_nr.to_s + "-"+ nr.to_s

    doc = Nokogiri::XML(open(full_url)) 

    number = doc.at('Number').text  
    operator = doc.at('Name').text 

    number_operator = "#{number}" + ";" + " #{operator}"

    number_operator 

    save = @opts.create(:numbers => number, :operator => operator) 
  end
end


推荐答案

问题出在这里:

que.rikt_nr = rikt_nr 
que.start_nr = start_nr  
que.end_nr = end_nr  
st_en = start_nr..end_nr

所以我把它改为:

Que.all.each do | que |

  url = "http://api.url.number=" 

  st_en = que.start_nr..que.end_nr 

  st_en.each do |nr| 
    full_url = "#{url}" + que.rikt_nr.to_s + "-"+ nr.to_s

    doc = Nokogiri::XML(open(full_url)) 

    number = doc.at('Number').text  
    operator = doc.at('Name').text 

    number_operator = "#{number}" + ";" + " #{operator}"

    number_operator 

    save = @opts.create(:numbers => number, :operator => operator) 

  end
end

现在可以工作。

这篇关于如何迭代所有记录并将数据库值传递给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:12