2014-10-17 21 views
0

主要的問題是,我的郵件沒有發送DJ不反序列化一個未保存的ActiveRecord模型的解決方法

我用「delayed_job_mongoid」軌3.2.19

要發送電子郵件,我使用

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.) 

所以我的延遲作業無法訪問合同ContactForm.class或應該呈現的文件

我是否需要它在我的初始化程序/ delayed_job.rb?我怎樣才能達到目的? ContactForm.class在模型目錄中

+0

什麼是整個錯誤消息? 'DocumentNotFound'表示找不到*文檔*,而不是類。這表明你的DJ有一個無效或缺少'_id'。 – 2014-10-17 22:59:42

+0

@ muistooshort這是什麼意思? – hodiecode 2014-10-17 23:17:16

+0

這意味着當'UserMailer.delay.question(@question)'時,'@ question.id'是'54418fe3c4bff8bb17000008',但當延遲的作業運行時,'id'沒有'ContactForm'。那麼是什麼破壞了ContactForm呢? – 2014-10-18 01:48:45

回答

0

好的,所以這是我的錯。我沒有carefuly閱讀文檔...

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 protected]" 

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

然後在我的配置/初始化/ delayed_job.rb我需要

require 'contact_form' 
require 'PassingData' 
相關問題