DJ没有对未保存的ActiveRecord模型解决方案进行反序列化

时间:2014-10-17 22:21:55

标签: ruby-on-rails ruby-on-rails-3 mongoid delayed-job

主要问题是我的电子邮件没有发送

我使用rails 3.2.19和'delayed_job_mongoid'

使用

发送电子邮件
UserMailer.delay.question(@question)

在我的数据库中注册了延迟的作业,但有错误

Mongoid::Errors::DocumentNotFound, class: ContactForm, primary key:
54418fe3c4bff8bb17000008 (Problem: Document(s) not found for class 
ContactForm with id(s) 54418fe3c4bff8bb17000008.
Summary:
When calling ContactForm.find with an id or array of ids, each 
parameter must match a document in the database or this error
will be raised. The search was for the id(s): 
54418fe3c4bff8bb17000008 ... (1 total) and the following ids were not found:  
54418fe3c4bff8bb17000008.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error 
configuration option to false, which will cause a nil to be returned instead of
raising this error when searching for a single id, or only the matched documents
when searching for multiples.)

所以我的延迟工作没有访问co ContactForm.class或应该呈现的文件

我的初始化器/ delayed_job.rb中是否需要它?我怎么能做到这一点? ContactForm.class位于模型目录

1 个答案:

答案 0 :(得分:0)

好的,这是我的错。我没有仔细阅读文件......

DJ没有对未保存的ActiveRecord模型进行反序列化 https://github.com/collectiveidea/delayed_job/wiki/common-problems

这是它的解决方法 http://www.kiprosh.com/blog/171

我最终得到了

控制器

require "PassingData"

class EmailsController < ApplicationController

    def ask_question

        @question = ContactForm.new(params)

        if @question.valid?

          contact_data = PassingData.serialize(@question)
          UserMailer.delay.question(contact_data)

       render stuff
    end
end

邮件收发器

class UserMailer < ActionMailer::Base
    default from: "email@email.com"

    def question(record)
      @record = PassingData.deserialize(record)
      mail(from: @record.email, to: "email@email.com", subject: 'Something')
  end
end

然后在我的config / initializers / delayed_job.rb中我需要

require 'contact_form'
require 'PassingData'