dynamic fields
By default Mongoid supports dynamic fields - that is it will allow attributes to get set and persisted on the document even if a field was not defined for them. There is a slight 'gotcha' however when dealing with dynamic attributes in that Mongoid is not completely lenient about the use of method_missing and breaking the public interface of the Document class.
When dealing with dynamic attributes the following rules apply:
If the attribute exists in the document, Mongoid will provide you with your standard getter and setter methods. For example, consider a person who has an attribute of "gender" set on the document:
# Set the person's gender to male. person[:gender] = "Male" person.gender = "Male" # Get the person's gender. person.gender
If the attribute does not already exist on the document, Mongoid will not provide you with the getters and setters and will enforce normal method_missing behavior. In this case you must use the other provided accessor methods: ([] and []=) or (read_attribute and write_attribute).
# Raise a NoMethodError if value isn't set. person.gender person.gender = "Male" # Retrieve a dynamic field safely. person[:gender] person.read_attribute(:gender) # Write a dynamic field safely. person[:gender] = "Male" person.write_attribute(:gender, "Male")
Dynamic attributes can be completely turned off by setting the Mongoid configuration option allow_dynamic_fields to false.