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

时间:2021-06-08 01:17:03

According to the ActiveJobs guide, section 8, it says:

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

This works with any class that mixes in GlobalID::Identification, which by default has been mixed into Active Model classes.

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

Mongoid::Document mixes ActiveModel::Model, but I can't find GlobalID::Identification in its included_modules.

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

  1. Where is GlobalID::Identification defined?

    GlobalID :: Identification在哪里定义?

  2. Can I effectively use any Mongoid::Document for my ActiveJobs?

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

3 个解决方案

#1


15  

There's a mistake in the guides. GlobalID::Identification has been mixed in ActiveRecord. If you mixin GlobalID::Identification into your mongoid documents it will work automatically as GID requires the instance to respond to id (returning the uniq identifier) and the class to respond to find (passing an id will return a record).

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

#2


7  

Put something like this in your initializer:

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

# 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

#3


6  

To provide more information to anyone having the same problem, you can make it work by simply adding GlobalID::Identification to your models.

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

class User
  include Mongoid::Document
  include GlobalID::Identification
end

I've actually done that by reopenning Mongoid::Document:

我实际上通过重新打开Mongoid :: Document来做到这一点:

module Mongoid::Document
  include GlobalID::Identification
end

However, I've got some really weird errors sometimes, with ActiveJob which didn't know how to serialize my models. I tried to debug it, but whenever I came into ActiveJob code I had:

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

pry> User.is_a? GlobalID::Identification
=> true

But ActiveJob::Arguments.serialize_argument didn't work as expected.

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

The workaround is also to reopen Mongoid::Relations::Proxy:

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

class Mongoid::Relations::Proxy
  include GlobalID::Identification
end

#1


15  

There's a mistake in the guides. GlobalID::Identification has been mixed in ActiveRecord. If you mixin GlobalID::Identification into your mongoid documents it will work automatically as GID requires the instance to respond to id (returning the uniq identifier) and the class to respond to find (passing an id will return a record).

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

#2


7  

Put something like this in your initializer:

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

# 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

#3


6  

To provide more information to anyone having the same problem, you can make it work by simply adding GlobalID::Identification to your models.

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

class User
  include Mongoid::Document
  include GlobalID::Identification
end

I've actually done that by reopenning Mongoid::Document:

我实际上通过重新打开Mongoid :: Document来做到这一点:

module Mongoid::Document
  include GlobalID::Identification
end

However, I've got some really weird errors sometimes, with ActiveJob which didn't know how to serialize my models. I tried to debug it, but whenever I came into ActiveJob code I had:

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

pry> User.is_a? GlobalID::Identification
=> true

But ActiveJob::Arguments.serialize_argument didn't work as expected.

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

The workaround is also to reopen Mongoid::Relations::Proxy:

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

class Mongoid::Relations::Proxy
  include GlobalID::Identification
end