如何遍历所有记录并将数据库值传递给变量



我有两个表,"Que"one_answers"Opts"。

我想遍历Que中的所有记录,并将它们添加到变量rikt_nrstart_nrend_nr中,因为它们将位于URL的末尾,看起来像:

api.url.number=8-00001

如何使它遍历Que并将rikt_nrstart_nrend_nr传递给代码的其余部分?

Que表有以下字段:

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有以下字段:

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

现在它起作用了。

相关内容

  • 没有找到相关文章