【问题标题】:DJ doesn't deserialize an unsaved ActiveRecord model workaroundDJ 不会反序列化未保存的 ActiveRecord 模型解决方法
【发布时间】:2014-12-13 13:55:09
【问题描述】:

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

我使用带有“delayed_job_mongoid”的 rails 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.)

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

我应该在我的 initializers/delayed_job.rb 中以某种方式要求它吗?我怎样才能做到这一点? ContactForm.class 在模型目录中

【问题讨论】:

  • 整个错误信息是什么? DocumentNotFound 表示找不到文档,而不是类。这表明您的 DJ 的_id 无效或缺失。
  • @muistooshort 是什么意思?
  • 这意味着当你UserMailer.delay.question(@question)@question.id54418fe3c4bff8bb17000008但是当延迟作业运行时没有ContactFormid。那么是什么破坏了ContactForms?

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


【解决方案1】:

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

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'

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 2016-06-15
    • 2012-04-16
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多