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

Persistence options not set when `with` called on embedded documents

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • None

      Hi,

      I have some problems changing persistence options for update operations involving embedded documents. I have to call parent.with(...) before issuing child.save - child.with(...).save does not work. I don't think that's the whole story, because even with this workaround I wasn't able to change the options for some of my update calls, but this is what I can reliably replicate at the moment.

      # Setup
      
      require 'mongoid'
      
      Mongoid.configure {|c| c.connect_to "mongoid_test"}
      
      class RootDocument
        include Mongoid::Document
        embeds_many :child_documents
      end
      RootDocument.delete_all
      
      class ChildDocument
        include Mongoid::Document
        embedded_in :root_document
      end
      
      Moped.logger = Logger.new(STDOUT)
      
      puts "Using Mongoid #{Mongoid::VERSION}"
      
      # Tests
      
      parent = RootDocument.new
      parent.with(safe: {w: 1}).save! # this `with` works
      
      child = parent.child_documents.build
      
      child.with(safe: {w: 1}).save! # this `with` is ignored
      
      child["a"] = 1
      
      parent.with(safe: {w: 0}) # this `with` works
      child.with(safe: {w: 1}).save! # this `with` is ignored
      

      Here's the output:

      Using Mongoid 3.1.5
        MOPED: 127.0.0.1:27017 INSERT       database=mongoid_test collection=root_documents documents=[{"_id"=>"528bc9a066d9f11c07000001"}] flags=[]
                               COMMAND      database=mongoid_test command={:getlasterror=>1, :w=>1} (0.6099ms)
        MOPED: 127.0.0.1:27017 UPDATE       database=mongoid_test collection=root_documents selector={"_id"=>"528bc9a066d9f11c07000001"} update={"$push"=>{"child_documents"=>{"_id"=>"528bc9a066d9f11c07000002"}}} flags=[] (0.1690ms)
        MOPED: 127.0.0.1:27017 UPDATE       database=mongoid_test collection=root_documents selector={"_id"=>"528bc9a066d9f11c07000001", "child_documents._id"=>"528bc9a066d9f11c07000002"} update={"$set"=>{"child_documents.$.a"=>1}} flags=[]
                               COMMAND      database=mongoid_test command={:getlasterror=>1, :w=>0} (0.6831ms)
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            amw Adam Wróbel
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: