关注“https://github.com/collectiveidea/delayed_job”中的delayed_job。我可以知道为什么我的delayed_job什么都不做?我跑了“rake jobs:work”广告得到了这些错误
错误:
Company#update_count_without_delay failed with TypeError: can't convert nil into String - 2 failed attempts
完成:
我遵循了这条指令:
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
答案 0 :(得分:1)
Company#update_count
出现错误。它应该是:
def update_count
uri = URI("http://graph.facebook.com/" + fbid)
data = Net::HTTP.get(uri)
# here you should use your local variable that you set in previous line
# instead of unset instance variable:
update_attribute(:count, JSON.parse(data)['count'])
end