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

HABTM doesn't work properly with identity map enabled

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

      The inverse relationship is not getting properly updated, as the sample code below shows. The inverse fieldname_ids list is updated, but not the actual fieldnames list itself.

      Observed with ruby 1.9.3p362 and mongoid 3.1.3.

      require 'mongoid'
      
      Mongoid.load!("mongoid.yml", :development)
      
      class Image
        include Mongoid::Document
        has_and_belongs_to_many :pages
      end
      
      class Page
          include Mongoid::Document
          has_and_belongs_to_many :images
      end
      
      i = Image.new
      i.save!
      p = Page.new
      p.images << i
      p.save!
      puts "Page has images.  count: #{p.images.length}"  # 1 - expected
      puts "Image has page ids stored: #{i.page_ids.length}"  # 1 - expected
      puts "Image should but doesn't have have page objects.  count: #{i.pages.length}"  # 0 - wrong. should be 1
      

      Simple mongoid.yml file:

      development:
        sessions:
          default:
            uri: "mongodb://localhost:27017/bug_demo"
            options:
              consistency: :strong
        options:
          identity_map_enabled: true
      

      Turning off the identity map fixes the problem. I'd be super happy to hear how I've missed something and that I can fix my code, but this looks to me like a bug in mongoid.

            Assignee:
            durran Durran Jordan
            Reporter:
            srleo srleo
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: