Validations
Mongoid includes ActiveModel::Validations
, so we have access to all the ActiveModel
validations that are available. In a nutshell, validations ensure the sanity of the data being stored. To quote a quick example:
class Author include Mongoid::Document field :name, type: String validates :name, presence: true end
This ensures that no author object will be created without a name. Similarly, there are options such as, :uniqueness
, :acceptance
, :associated
, :confirmation
, which are from ActiveModel::Validations
, that allow checks for uniqueness of attributes, presence of associated models, and so on. The common options used along with these validations are :if
, :allow_nil
, and :on
. You can find a lot more details about validations at http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html.