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

When changing a document's _type, relations which hold that document can't be reloaded properly

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.22
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I'm seeing some confusing behavior where the IdentityMap seems to hold on to the class of given objects in a relationship, such that if the object changes _type, no amount of reloading the relationship will cause it to reload. See below for examples:

       ruby
      class Band
        include Mongoid::Document
        has_many :musicians
      end
      
      class RockBand < Band
      end
      
      class JazzBand < Band
      end
      
      class Musician
        include Mongoid::Document
        belongs_to :band
      end
      
      b = RockBand.create()
      m = Musician.create(:band_id => b.id)
      b.write_attribute('_type', JazzBand.name)
      b.save()
      b.reload()
      b.class
      # => RockBand
      
      m.band.reload().class
      # => RockBand
      
      m.reload().band.class
      # => RockBand
      
      m.reload().band.reload().class
      # => RockBand
      
      Mongoid::IdentityMap.clear()
      m.reload().band.class
      # => JazzBand
      

      It seems like the m.reload().band.class should cause the musician to be reloaded so it picks up that the band on the other side of the relationship is now a JazzBand.

            Assignee:
            durran Durran Jordan
            Reporter:
            froodian Ian Leue
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: