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

Dup/clone of a document that has old fields causes an error unless you include Mongoid::Attributes::Dynamic

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

      One of the greatest things about MongoDB is the flexible schema. If you have a model that has some fields and you later decide to remove those fields without doing a migration and $unset'ing them from the documents, you will get an error if you later dup or clone the document:

      class Foo
        include Mongoid::Document
        field :name, :type => String
      end
      
      f = Foo.create
      Foo.collection.find({:_id => f.id}).update(:$set => {:old_property => "value"})
      f.reload()
      f.dup
      
      Mongoid::Errors::UnknownAttribute:
      Problem:
        Attempted to set a value for 'old_property' which is not allowed on the model Foo.
      Summary:
        Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Foo#old_property= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError.
      Resolution:
        You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/attributes/processing.rb:100:in `process_attribute'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/attributes/processing.rb:25:in `block in process_attributes'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/attributes/processing.rb:23:in `each_pair'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/attributes/processing.rb:23:in `process_attributes'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/document.rb:110:in `block in initialize'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/document.rb:104:in `initialize'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/copyable.rb:25:in `new'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-4294ce79f6aa/lib/mongoid/copyable.rb:25:in `clone'
      	from (irb):23
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
      	from /Users/jonathan/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
      	from bin/rails:4:in `require'
      	from bin/rails:4:in `<main>'2.1.5 :024 >
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            jonhyman Jon Hyman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: