Validation¶
On this page
Mongoid includes ActiveModel::Validations
to supply the basic
validation plus an additional associated and uniqueness validator.
See the Active Record Validations Rails guide and ActiveModel::Validations documentation for more information.
Mongoid behaves slightly different to Active Record when using #valid?
on already persisted data. Active Record’s #valid?
will run all
validations whereas Mongoid’s #valid?
will only run validations on
documents that are in memory as an optimization.
validates_uniqueness_of
and :conditions
Option¶
The :conditions
option to validates_uniqueness_of
can be used to
provide additional conditions to add to the database query looking for
identical documents. This option does not influence when the validation
is executed because it is not considered when Mongoid retrieves the present
value of the respective field from the model. Consider the following example: