ARTICLE AD BOX
I'm working on a Rails application containing several models based on Mongo documents.
I'm eventually bumping into an Mongoid::Errors::UnknownAttribute exception when mongoid Copyable#clone method is called by an instance of one of these models.
object = MyModel.find "123" object.clone /usr/local/bundle/gems/mongoid-7.4.3/lib/mongoid/attributes/processing.rb:90:in `process_attribute': (Mongoid::Errors::UnknownAttribute) message: Attempted to set a value for 'my_field' which is not allowed on the model MyModel. summary: MyModel#my_field= was called but there is no 'my_field' field defined in the model, and Mongoid::Attributes::Dynamic is not included. This error is also raised instead of NoMethodError if the unknown attribute is passed to any method that accepts an attributes hash, such as #attributes=. resolution: Define the field 'my_field' in MyModel, or include Mongoid::Attributes::Dynamic in MyModel if you intend to store values in fields that are not explicitly definedThis happens because some fields were deleted from the application layer years ago, but they were never unset from the Mongo document in the database level.
If I explicitly unset them:
object.unset("my_field")That's enough for me to get rid of the error and it's possible to run object.clone without any errors. However, spotting all the deprecated fields for every model is becoming quite tedious.
Is there a way to copy all the attributes from a Mongo document and its embedded documents while ignoring any fields that are not defined on the application level anymore?
6456 silver badges25 bronze badges
Explore related questions
See similar questions with these tags.
