Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mongoid::Document是ActiveJobs的GlobalID::标识吗?_Ruby On Rails_Ruby_Mongoid_Activemodel_Rails Activejob - Fatal编程技术网

Ruby on rails Mongoid::Document是ActiveJobs的GlobalID::标识吗?

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已在

根据第8节的规定,它说:

这适用于任何混入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