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

Reference relations are reset after a build, save

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

      I'm seeing that if I have a has_many reference relation and I save something in the collection, it resets the collection after the save forcing me to reload the collection to retrieve the new item. Example:

      class Book
        include Mongoid::Document
        
        field :name
        has_many :pages
      end
      
      class Page
        include Mongoid::Document
        belongs_to :book
        
        field :text
      end
      
      book = Book.create(:name => "A Tale of Two Cities")
      book.pages.build(:text => "It was the best of times, it was the worst of times.")
      book.pages.length 
      # 1 
      book.pages.count
      # 0
      book.pages[0].save
      # true
      book.pages.length
      # 0
      book.pages
      # []
      book.pages.count
      # 1
      

      I either have to execute book.reload or book.pages(true) in order to get the page. This does not happen with embedded documents as far as I can tell.

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

              Created:
              Updated:
              Resolved: