Is Mongoid::Document a GlobalID::Identification for ActiveJobs?
Asked Answered
B

3

10

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

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

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

  1. Where is GlobalID::Identification defined?

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

Brandwein answered 7/1, 2015 at 13:48 Comment(0)
N
16

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).

Noiseless answered 7/1, 2015 at 14:29 Comment(1)
In case it will help someone else, you "mixin" by adding include GlobalID::Identification to the top of your model.Prickle
M
10

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
Magazine answered 24/3, 2016 at 9:45 Comment(2)
I get uninitialized constant Mongoid::Relations when I try this.Trifolium
For mongoid >= 7 it's Mongoid::Association::Proxy instead of Mongoid::Relations::ProxyOrit
B
8

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

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

I've actually done that by reopenning 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:

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

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

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

class Mongoid::Relations::Proxy
  include GlobalID::Identification
end
Brandwein answered 1/2, 2015 at 14:38 Comment(6)
Can you reproduce the SerializationError bug?Noiseless
I've already tried with a simple application but it didn't work. I'm having this problem in a complex application but I haven't found any reason for this bug to happen. In the engine with this code, it works well, but when I use it inside a Rails app, I can see that my models have GlobalID::Identification but some of them fail to serialize correctlyBrandwein
I fought with this for a while, and what I saw was that an object pulled off a belongs_to association was not reporting as being a GlobalID::Identification object. My fix was to also include GlobalID::Identification into Mongoid::Relations::Proxy. I believe its related to mongoid's use of marshalable, but I'm not entirely sure.Gains
Thanks for your comment @c.apolzon, I'll try that soon and add it to my post if it works.Brandwein
This code should be put in an initializer I guess ?Predigestion
@CyrilDD I actually load it from another (private) gem which handles all ActiveJob stuff. But you can do it in an initializer also !Brandwein

© 2022 - 2024 — McMap. All rights reserved.