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

The use runtime persistence raises an error for updates

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.0.1
    • Affects Version/s: 6.4.8
    • Component/s: Persistence
    • Labels:
      None
    • Fully Compatible

      The use runtime persistence options causes the following error when trying to update a document:

       

      The option database cannot be set on a new collection instance. The options that can be updated are [:read, :read_concern, :write, :write_concern] (Mongo::Error::UnchangeableCollectionOption)

       

      Here is a snippet to reproduce the error:
       

      require 'mongoid' 
      
      Mongoid.configure do |config| 
        config.connect_to('main') 
      end 
      
      class User 
        include Mongoid::Document 
        field:name, type: String 
      end 
      
      user = User.new(name: 'John') 
      user.with(database: 'alt') do |u| 
        u.save! 
      end 
      
      # 1st try: reusing the same user object 
      user.with(database: 'alt') do |u|   
        u.update(name:'John Doe 2') # raises Mongo::Error::UnchangeableCollectionOption 
      end 
      
      # 2nd try: using a new user object 
      User.with(database: 'alt') do |clazz| 
        u=clazz.find_by(name:'John') 
        u.update(name:'John Doe 1') # raises Mongo::Error::UnchangeableCollectionOption 
      end

      The 1st try uses the code suggested in the documentation: https://www.mongodb.com/docs/mongoid/6.4/tutorials/mongoid-persistence/#runtime-persistence-options
       
       

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            rodrigorcomp@gmail.com Rodrigo R Aquino
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: