DelayedJob:如何解决“作业无法加载”的问题?

我正在使用Ruby on Rails 3.1.0和DelayedJob。 网络上的很多人都得到了“ Job failed to load: uninitialized constant Syck::Syck ”错误,但我认为我至少发现了产生错误的内容(在我的情况下)。 我有一个类似以下的ActiveModel:

 class Contact include ActiveModel::Conversion include ActiveModel::Validations include ActiveModel::Dirty extend ActiveModel::Naming extend ActiveModel::Translation attr_accessor :full_name, :email, :subject, :message def initialize(attributes = {}) attributes.keys.each do |attr| instance_variable_set "@" + attr.to_s, attributes[attr.to_sym] end end validates_presence_of :full_name, :email, :subject, :message def persist @persisted = true end def persisted? false end end 

相关的控制器操作是:

 def contact @contact = Contact.new(params[:contact]) if @contact.valid? ::Contact::Mailer.delay.contact(@contact) respond_to do |format| format.html { redirect_to root_path } end else respond_to do |format| format.html { render :action => :contact } end end end 

我注意到我的“着名”“臭名昭着”的Job failed to load: uninitialized constant Syck::Syck只有在我运行@contact.valid?时才会发生@contact.valid? 。 如果我像这样重新实现上面的控制器动作:

 def contact @contact = Contact.new(params[:contact]) ::Contact::Mailer.delay.contact(@contact) respond_to do |format| format.html { redirect_to root_path } end end 

所有工作都按预期工作:我没有收到错误,电子邮件已成功发送。 简而言之,当我运行@contact.valid? 在控制器动作内部 (我也可以在不使用if ... else语句的情况下运行), 它会生成Job failed to load错误 。 我真的不明白这个与DelayedJob gem有关的奇怪行为和valid? 方法。

为什么会这样? 我该如何解决这个问题?


有关 DelayedJob的 更多信息 :“作业未能加载:未初始化的常量Syck :: Syck”


更新

如果我在使用或不使用@contact.valid? 情况下调试@contact.valid? 方法…

…当我使用@contact.valid? 方法(DelayedJob不起作用)我明白了

 #<ActiveModel::Errors:0x00000101759408 @base=#<Contact:0x000001017597f0 @full_name="Sample name", @email="foo@bar.com", @subject="Sample subject", @message="Sample message content.", @validation_context=nil, @errors=#>, @messages={}> 

…当使用@contact.valid? 方法(DelayedJob工作)我得到

 #<ActiveModel::Errors:0x00000101759408 @base=#<Contact:0x000001017597f0 @full_name="Sample name", @email="foo@bar.com", @subject="Sample subject", @message="Sample message content.", @errors=#>, @messages={}> 

请注意,在第二种情况下, @validation_context=nil不存在,并且在两种情况下都存在“嵌套” 语句。 那是一个错误吗?

我发现了一个适合我的解决方案。 您可以在Contact类中重新定义’Object#to_yaml_properties’方法,以仅包含所需的属性。 从而排除’错误’变量。

 def to_yaml_properties ['@full_name', '@email', '@subject', '@message'] end 

希望这可以帮助。