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

Runtime persistence options

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 6.4.8
    • Component/s: Persistence
    • None

      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:
            Unassigned Unassigned
            Reporter:
            rodrigorcomp@gmail.com Rodrigo R Aquino
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: