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

In certain situations, concat to create embedded models does not persist but create does with identity map enabled.

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

      I have an <code>after_create</code> callback for one of my embedded models which updates data in the model (well, it queues up a background job but in dev/test it all happens in serial). When the identity map is enabled, the data does not persist if I <code>concat</code> or <code><<</code> the embedded model into the parent but does persist when I use <code>create</code>.

      Example:

      class Book
        include Mongoid::Document
        embeds_many :pages
      end
      
      class Page
        include Mongoid::Document
        embedded_in :book
        field :text
        
        after_create :update_something
        
        def update_something
          update_attributes(:text => "test")
        end
      end
      
      # Ensure that we have an empty identity map
      Mongoid::IdentityMap.clear
      Book.destroy_all
      
      b = Book.create
      b.pages << Page.new
      b.pages
      # => [#<Page _id: ...>]
      
      # Clear out the identity map to ensure the next find call hits the database
      Mongoid::IdentityMap.clear
      Book.find(b.id).pages
      # => []
      # would have expected this to contain a Page
      

      If I change <code>b.pages << Page.new</code> to <code>b.pages.create</code>, everything works fine. Alternatively, I can call <code>Mongoid::IdentityMap.clear</code> in the callback and it'll work regardless of how the create occurs.

      Using Mongoid 2.3.0.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jonhyman Jon Hyman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: