我们在ip_logs
的延迟作业中执行了一些第三方API内容,所有内容都在after_save
中:
class IpLog < ActiveRecord::Base
after_save :process_request
def process_request
# stuff that doesn't need to be delayed
self.delay.process_request_delayed(remote_ip, request_data)
end
def process_request_delayed(remote_ip, data)
# third party API stuff
end
end
今天我向process_request_delayed
添加了一行代码,在我收到API调用的结果后运行:
def process_request_delayed(remote_ip, data)
# third party API stuff
check_for_fraud_based_on_ip_results
end
我还在IpLog模型中添加了一个虚拟属性:
class IpLog < ActiveRecord::Base
attr_accessor :new_user
end
然后我从delayed_job收到Jobs cannot be created for records before they\'ve been persisted
个错误。经过多次印刷声明之后,我将其深入到这一行:
self.delay.process_request_delayed(remote_ip, request_data)
根本不会输入process_request_delayed
。
我挖到了delayed_job的源代码,发现在这种情况下发生了这个错误:
if object.respond_to?(:new_record?) && object.new_record?
raise(ArgumentError, "Jobs cannot be created for records before they\'ve been persisted")
end
由于process_request_delayed
在after_save中运行,我认为ip_log
对象的不是new_record?
。但无论如何我检查了,我得到了这个令人费解的截图:
(抱歉停电;我不允许透露业务逻辑)
我进一步将有问题的代码深入到UsersController
中的这一行:
@ip_log = IpLog.new()
@ip_log.new_user = true // take this line out and all is well
@ip_log.save
那么,为什么你认为这是?我们使用Rails 3.2.17和delayed_job_active_record(0.3.3)。