Ruby on rails Mongoid::Document是ActiveJobs的GlobalID::标识吗?
根据第8节的规定,它说: 这适用于任何混入GlobalID::Identification的类, 默认情况下,已将其混合到活动模型类中Ruby on rails Mongoid::Document是ActiveJobs的GlobalID::标识吗?,ruby-on-rails,ruby,mongoid,activemodel,rails-activejob,Ruby On Rails,Ruby,Mongoid,Activemodel,Rails Activejob,根据第8节的规定,它说: 这适用于任何混入GlobalID::Identification的类, 默认情况下,已将其混合到活动模型类中 Mongoid::Document混合了ActiveModel::Model,但我在其包含的模块中找不到GlobalID::Identification 在哪里定义了GlobalID::Identification 我能否有效地将任何Mongoid::Document用于我的ActiveJobs 指南上有个错误GlobalID::Identification已在
Mongoid::Document
混合了ActiveModel::Model
,但我在其包含的模块中找不到GlobalID::Identification
GlobalID::Identification
Mongoid::Document
用于我的ActiveJobs指南上有个错误
GlobalID::Identification
已在ActiveRecord
中混合使用。如果将GlobalID::Identification
混合到mongoid文档中,它将自动工作,因为GID要求实例响应id
(返回uniq标识符),而要响应find
(传递id
将返回一条记录).要向任何有相同问题的人提供更多信息,只需将GlobalID::Identification
添加到模型中即可
class User
include Mongoid::Document
include GlobalID::Identification
end
实际上,我已经通过重新打开Mongoid::Document
:
module Mongoid::Document
include GlobalID::Identification
end
然而,我有时会遇到一些非常奇怪的错误,ActiveJob
不知道如何序列化我的模型。我试图调试它,但每当我进入ActiveJob
code时,我都会:
pry> User.is_a? GlobalID::Identification
=> true
但没有像预期的那样起作用
解决方法还包括重新打开Mongoid::Relations::Proxy
:
class Mongoid::Relations::Proxy
include GlobalID::Identification
end
在初始值设定项中放入类似的内容:
# 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
为了防止它对其他人有帮助,您可以通过在模型顶部添加
include GlobalID::Identification
来“混合”。您能否重现SerializationError
错误?我已经尝试了一个简单的应用程序,但它不起作用。我在一个复杂的应用程序中遇到了这个问题,但我还没有找到这个错误发生的任何原因。在使用此代码的引擎中,它工作得很好,但当我在Rails应用程序中使用它时,我可以看到我的模型具有GlobalID::Identification
,但其中一些无法正确序列化我为此奋斗了一段时间,我看到的是,从关联中提取的对象没有报告为GlobalID::Identification
对象。我的解决方案是将GlobalID::Identification
加入Mongoid::Relations::Proxy
。我相信这与mongoid使用Marshable有关,但我不能完全确定。谢谢你的评论@c.apolzon,我很快就会尝试,如果有用的话,我会把它添加到我的帖子中。我想这段代码应该放在一个初始值设定项中?当我尝试这段代码时,我得到了未初始化的常量Mongoid::Relations
。对于Mongoid>=7
它是Mongoid::Association::Proxy
而不是Mongoid::Relations::Proxy