Mongoid :: Document是GlobalID :: GlobalJobs的标识吗?

Geo*_*roy 9 ruby ruby-on-rails activemodel mongoid rails-activejob

根据ActiveJobs指南第8节,它说:

这适用于在GlobalID :: Identification中混合的任何类,默认情况下,它已混合到Active Model类中.

Mongoid::Document混合ActiveModel::Model,但我GlobalID::Identification在其included_modules中找不到.

  1. 在哪里GlobalID::Identification定义?

  2. 我可以有效地使用任何Mongoid::Document我的ActiveJobs吗?

bcd*_*bcd 15

指南中有一个错误.GlobalID::Identification已经混入了ActiveRecord.如果你混入GlobalID::Identification你的mongoid文档,它将自动工作,因为GID要求实例响应id(返回uniq标识符)和类来响应find(传递一个id将返回一个记录).

  • 如果它会帮助其他人,你可以通过在模型的顶部添加`include GlobalID :: Identification`来"混合". (8认同)

san*_*rom 8

在你的初始化器中加入这样的东西:

# config/initalizers/mongoid.rb

if defined?(Mongoid)
  # GlobalID is used by ActiveJob (among other things)
  # https://github.com/rails/globalid

  Mongoid::Document.send(:include, GlobalID::Identification)
  Mongoid::Relations::Proxy.send(:include, GlobalID::Identification)
end
Run Code Online (Sandbox Code Playgroud)

  • 对于 `mongoid >= 7` 它是 `Mongoid::Association::Proxy` 而不是 `Mongoid::Relations::Proxy` (3认同)

Geo*_*roy 6

要向遇到相同问题的任何人提供更多信息,只需添加GlobalID::Identification到模型中即可使其工作.

class User
  include Mongoid::Document
  include GlobalID::Identification
end
Run Code Online (Sandbox Code Playgroud)

我实际上是通过重新打开来完成的Mongoid::Document:

module Mongoid::Document
  include GlobalID::Identification
end
Run Code Online (Sandbox Code Playgroud)

但是,我有时会遇到一些非常奇怪的错误,因为ActiveJob它们不知道如何序列化我的模型.我尝试调试它,但每当我进入ActiveJob代码时,我有:

pry> User.is_a? GlobalID::Identification
=> true
Run Code Online (Sandbox Code Playgroud)

但是ActiveJob :: Arguments.serialize_argument没有按预期工作.

解决方法也是重新打开Mongoid::Relations::Proxy:

class Mongoid::Relations::Proxy
  include GlobalID::Identification
end
Run Code Online (Sandbox Code Playgroud)

  • 我和它争吵了一段时间,我看到的是一个从belongs_to协会中删除的对象没有报告为"GlobalID :: Identification"对象.我的解决方法是将`GlobalID :: Identification`包含在`Mongoid :: Relations :: Proxy`中.我认为它与mongoid使用marshalable有关,但我不完全确定. (4认同)