我正在使用 mongodb 开发 ROR 应用程序。基本上我的模型课之一是这样的:
require 'open-uri'
class StockPrice
include Mongoid::Document
include Mongoid::Timestamps
field :stock_name, type: String
field :price, type: Float
index :stock_name => +1, :updated_at => -1
def self.price(stock_name)
#print stock_name
g=StockPrice.new
g.crawl(stock_name)
where(stock_name: stock_name).desc(:updated_at).first.price
end
#Search for the stock in screener.in and get its last price.
def crawl(stock_name)
company_name=stock_name
agent = Mechanize.new
page = agent.get('http://www.screener.in/')
form = agent.page.forms[0]
agent.page.forms[0]["q"]=company_name
button = agent.page.forms[0].button_with(:value => "Search Company")
pages=agent.submit(form, button)
new_page=pages.uri.to_s
doc=Nokogiri::HTML(open(new_page))
row_data = doc.css('.table.draggable.table-striped.table-hover tr.strong td').map do |tdata|
tdata.text
end
g=row_data[2]
StockPrice.create(stock_name:stock_name,price:g)
end
handle_asynchronously :crawl
end
现在在我的应用程序中,我想使用延迟的作业运行爬网功能,以便它可以在后台运行为此我正在使用延迟的作业_mongoid gem。但是当我使用 handle_asynchronously 时出现此错误:在记录被持久化之前无法为记录创建作业