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

Respect persistence options on auto-saved associations

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.1
    • Affects Version/s: None
    • Component/s: Persistence
    • None
    • Minor Change

      class Parent
        include Mongoid::Document
        has_many :children, autosave: true
      end
      
      class Child
        include Mongoid::Document
        belongs_to :parent
      end
      
      parent = Parent.new
      child = Child.new
      
      child.with(collection: 'other-children') do |child|
        parent.with(collection: 'other-parents') do |parent|
          child.parent = parent
          parent.save!
        end
      end
      

      This code will generate following commands:

      {"insert"=>"other-parents", "ordered"=>true, "documents"=>[{"_id"=>BSON::ObjectId('61545d4dc08a6e73ddd9f1dd')}], "$db"=>"mg_blog_test", "lsid"=>{"id"=><BSON::Binary:0x10680 type=uuid data=0x07adf65b5a2f43ae...>}}
      
      {"insert"=>"other-parents", "ordered"=>true, "documents"=>[{"_id"=>BSON::ObjectId('61545d4dc08a6e73ddd9f1dc'), "parent_id"=>BSON::ObjectId('61545d4dc08a6e73ddd9f1dd')}], "$db"=>"mg_blog_test", "lsid"=>{"id"=><BSON::Binary:0x10680 type=uuid data=0x07ad...
      

      We can see that both documents are inserted into other-parents collection, while there should be one insertion into other-parents, and one into other-children.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: