Module | Mongoid::Document |
In: |
lib/mongoid/document.rb
lib/mongoid/railties/document.rb |
This is the base module for all domain objects that need to be persisted to the database as documents.
new_record | [R] |
Instantiate a new Document, setting the Document‘s attributes if given. If no attributes are provided, they will be initialized with an empty Hash.
If a primary key is defined, the document‘s id will be set to that key, otherwise it will be set to a fresh +BSON::ObjectId+ string.
@example Create a new document.
Person.new(:title => "Sir")
@param [ Hash ] attrs The attributes to set up the document with. @param [ Hash ] options A mass-assignment protection options. Supports
:as and :without_protection
Performs equality checking on the document ids. For more robust equality checking please override this method.
@example Compare for equality.
document == other
@param [ Document, Object ] other The other object to compare with.
@return [ true, false ] True if the ids are equal, false if not.
Performs class equality checking.
@example Compare the classes.
document === other
@param [ Document, Object ] other The other object to compare with.
@return [ true, false ] True if the classes are equal, false if not.
Used in conjunction with fields_for to build a form element for the destruction of this association. Always returns false because Mongoid only supports immediate deletion of associations.
See ActionView::Helpers::FormHelper::fields_for for more info.
Returns an instance of the specified class with the attributes and errors of the current document.
@example Return a subclass document as a superclass instance.
manager.becomes(Person)
@raise [ ArgumentError ] If the class doesn‘t include Mongoid::Document
@param [ Class ] klass The class to become.
@return [ Document ] An instance of the specified class.
Delegates to ==. Used when needing checks in hashes.
@example Perform equality checking.
document.eql?(other)
@param [ Document, Object ] other The object to check against.
@return [ true, false ] True if equal, false if not.
Freezes the internal attributes of the document.
@example Freeze the document
document.freeze
@return [ Document ] The document.
@since 2.0.0
Checks if the document is frozen
@example Check if frozen
document.frozen?
@return [ true, false ] True if frozen, else false.
@since 2.0.0
Generate an id for this Document.
@example Create the id.
person.identify
@return [ BSON::ObjectId, String ] A newly created id.