Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4590

Association proxy objects are not correctly compared with === on RHS

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 6.4.1, 7.0.1
    • Component/s: None
    • None
    • Environment:
      ruby 2.3.1 / rails 5.2 / ubuntu latest / mongo latest / mongoid 7.0.1

      Greetings,

      I ran into a ActiveJob Serialization for a mongoid class that has it's roots here:

      https://github.com/rails/rails/blob/5-2-stable/activejob/lib/active_job/arguments.rb#L59

      The issue appears that while getting an document "directly" by asking for it by id, or creating it, the case statement in #59 works fine.    If you ask for a document in-directly, such as by traveling though it's relationships, the case statement does not work.

      FYI: the case/when statement uses a === for class comparisions

      Following this issue, the code below produces the error:

      Mongoid.load!("config/mongoid.yml", :development)
      
      class BaseDoc
       include Mongoid::Document
       include GlobalID::Identification
      
      has_one :child, class_name: 'ChildDoc', inverse_of: :parent
      
      field :data, type: String
      end
      
      class ChildDoc
       include Mongoid::Document
       include GlobalID::Identification
      
      belongs_to :parent, class_name: 'BaseDoc', inverse_of: :child, optional: true
      
      field :data, type: String
      end
      
      base = BaseDoc.create(data: 'dog')
      child = ChildDoc.create(data: 'cat')
      child.parent = base
      child.save
      
      puts "GlobalID::Identification === base is #\{GlobalID::Identification === base}"
      puts "GlobalID::Identification === child is #\{GlobalID::Identification === child}"
      
      base2 = ChildDoc.where(data: 'cat').first.parent
      
      puts "GlobalID::Identification === base2 is #\{GlobalID::Identification === base2}"
      puts "base2.is_a?(GlobalID::Identification) is #\{base2.is_a?(GlobalID::Identification)}"
      

      The exciting part, is the 3rd "puts", which returns false.

      As far as I can tell, the objects are identical. 

      Because GlobalID::Identification === base2 is false, the ActiveJob::Serializer will throw up an exception at line 59, which will not catch it for base2

      I have this setup on a digitalocean machine, if you give me your public key, I can give you access to the machine and you can scratch your head too.

       

      Regards,

       

      Daniel Lo

       

      config/mongoid.yml

      development: 
       clients: 
       default: 
       database: mongoid
       hosts: 
       - localhost:27017
      

       

       

            Assignee:
            sam.rossi@mongodb.com Samuel Rossi (Inactive)
            Reporter:
            wilburlo123 Daniel Lo
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: