从“ https://github.com/collectiveidea/delayed_job ”关注delayed_job 。我可以知道为什么我的延迟工作什么都不做吗?我跑了“rake jobs:work”广告得到了这些错误
错误:
Company#update_count_without_delay failed with TypeError: can't convert nil into String - 2 failed attempts
完毕:
- 在 gemfile 中添加了“gem 'delayed_job_active_record'”
- 在控制台中:“rails 生成延迟作业:活动记录”
- 在控制台中:“rake db:migrate”
我遵循了这个指示:
If a method should always be run in the background, you can call #handle_asynchronously after the method declaration:
class Device
def deliver
# long running method
end
handle_asynchronously :deliver
end
device = Device.new
device.deliver
模型:
require 'json'
require 'net/http'
require 'rubygems'
require 'delayed_job'
class Company < ActiveRecord::Base
before_save :validate_fbid
scope :toplikes, order("count desc").limit(20)
attr_accessible :desc, :fbid, :name, :url, :count
url_regex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
validates :name, :presence => true
validates :url, :presence => true,
:format => { :with => url_regex },
:uniqueness => { :case_sensitive => false }
validates :fbid, :presence => true
validates :desc, :presence => true
def update_count
uri = URI("http://graph.facebook.com/" + fbid)
data = Net::HTTP.get(uri)
self.count = JSON.parse(data)['likes']
end
handle_asynchronously :update_count
end
控制器:
def create
company = Company.new(params[:company])
if company.save
@message = "New company created."
company.update_count
redirect_to root_path
else
@message = "Company create attempt failed. Please try again."
redirect_to new_path
end
end
- 在模型顶部添加了“需要'delayed_job'”
- 重新启动我的服务器