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

Nested documents are saved under the wrong parent

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

      Hi,

      When saving documents with 3 levels of nesting, child objects are saved under the wrong parent :

      user = User.create
      website = user.websites.create
      post = website.posts.create
      post2 = website.posts.create
      
      post.images.create
      post2.images.create
      
      puts "#{user.to_json}"
      puts "#{user.reload.to_json}"
      

      Each post should have an image, that's true on the "unsaved" user (

      Unable to find source-code formatter for language: user. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      .to_json

      )
      => https://gist.github.com/vdaubry/cdc465d6d5ef84576830

      But when i reload the user all images are embedded under the first post (

      Unable to find source-code formatter for language: user. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      .reload.to_json

      )
      => https://gist.github.com/vdaubry/a9c217a467dd9ff9a7fb

      Is it a bug or i am missing something obvious ?

      Here are the class definition used to reproduce this :

      class User
        include Mongoid::Document
        embeds_many :websites
      end
      
      class Website
        include Mongoid::Document
        include Mongoid::Timestamps
        embedded_in :user
        embeds_many :posts
      
      end
      
      class Post
        include Mongoid::Document
        include Mongoid::Timestamps
      
        embedded_in :website
        embeds_many :images
      end
      
      class Image
        include Mongoid::Document
        include Mongoid::Timestamps
        embedded_in :post
      end
      

      My gemfile :

      ruby 2.1.2p95
      gem 'rails', '~> 4.1.4'
      gem 'mongoid', '~> 4.0.0'
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            vdaubry vincent daubry
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: