我正在尝试使用resque gem生成xml文件作为后台任务,这里是生成xml文件的类....
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV']
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
require 'rubygems'
require 'builder'
class GenerateXml
@queue = :worker
def self.perform
file_name = "#{Time.now.strftime('%Y_%m_%d')}_export.xml"
xml = Builder::XmlMarkup.new(:target => file_name, :indent => 2)
xml.instruct!(:xml, :encoding => "UTF-8")
xml.Postings do
Result.find_in_batches do |result|
xml.Posting do
xml.JobID result.job_id
xml.JobURL result.job_url
end
end
end
end
end
我从中提取xml标记字段的结果模型就像这样
require 'active_record'
class Result < ActiveRecord::Base
def job_id
self.link
end
def job_url
self.link
end
end
我的问题是为什么来自班级GenerateXml
的result.job_url和result.job_id无法正确映射到Result
班级?
还有一件事Result
类在app / models目录下,其中GenerateXml
类位于lib / jobs目录下
这是错误的痕迹。
undefined method `job_id' for #<Array:0xb3f26738>
/home/netserv/RubymineProjects/crawler/lib/jobs/generate_xml.rb:22:in `perform'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/builder-3.0.0/lib/builder/xmlbase.rb:155:in `call'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/builder-3.0.0/lib/builder/xmlbase.rb:155:in `_nested_structures'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/builder-3.0.0/lib/builder/xmlbase.rb:63:in `method_missing'
/home/netserv/RubymineProjects/crawler/lib/jobs/generate_xml.rb:21:in `perform'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.3/lib/active_record/relation/batches.rb:72:in `find_in_batches'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.3/lib/active_record/querying.rb:8:in `__send__'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/activerecord-3.2.3/lib/active_record/querying.rb:8:in `find_in_batches'
/home/netserv/RubymineProjects/crawler/lib/jobs/generate_xml.rb:20:in `perform'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/builder-3.0.0/lib/builder/xmlbase.rb:155:in `call'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/builder-3.0.0/lib/builder/xmlbase.rb:155:in `_nested_structures'
/home/netserv/.rvm/gems/ruby-1.8.7-p357/gems/builder-3.0.0/lib/builder/xmlbase.rb:63:in `method_missing'
/home/netserv/RubymineProjects/crawler/lib/jobs/generate_xml.rb:19:in `perform'
答案 0 :(得分:2)
find_in_batches
returns arrays。在Postings
区块内,您可以执行此操作:
Result.find_in_batches do |results|
results.each do
xml.Posting do
xml.JobID result.job_id
xml.JobURL result.job_url
end
end
end
但为什么甚至打扰find_in_batches
?
答案 1 :(得分:0)
find_in_batches
是returning an Array。
您必须更新或创建特定的Result
,而不是更新Array
Results
,这意味着什么。