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?

Jaffa
  • 12,442
  • 4
  • 49
  • 101

3 Answers3

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

Cristian Bica
  • 4,067
  • 27
  • 28
  • 8
    In case it will help someone else, you "mixin" by adding `include GlobalID::Identification` to the top of your model. – SteveO7 Jan 14 '15 at 02:29
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
sandstrom
  • 14,554
  • 7
  • 65
  • 62
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
Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • Can you reproduce the `SerializationError` bug? – Cristian Bica Feb 02 '15 at 11:59
  • 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 correctly – Jaffa Feb 02 '15 at 12:37
  • 5
    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. – c.apolzon Mar 04 '15 at 01:33
  • Thanks for your comment @c.apolzon, I'll try that soon and add it to my post if it works. – Jaffa Mar 04 '15 at 12:18
  • This code should be put in an initializer I guess ? – Cyril Duchon-Doris Jul 29 '15 at 17:12
  • @CyrilDD I actually load it from another (private) gem which handles all ActiveJob stuff. But you can do it in an initializer also ! – Jaffa Jul 31 '15 at 06:53