Reputation: 380
I have user model
class User
include MongoMapper::Document
key :phone, Integer, :required => true, :unique => true
key :is_confirmed, Boolean, :default => false
timestamps!
end
and validate uniqueness of phone but i can create user with the same phone without error.WHY?
why validate uniqueness doesn't work
Upvotes: 0
Views: 89
Reputation: 10251
MongoMapper uses ActiveModel:Validations, so it works almost exactly like ActiveRecord
Try this to validate: validates_uniqueness_of
validates_uniqueness_of :phone
Validations are run when attempting to save a record. If validations fail
, save will return false
.
Most Simple Validations can be declared along with the keys.
Example:
class Person
include MongoMapper::Document
key :first_name, String, :required => true
key :last_name, String, :required => true
key :age, Integer, :numeric => true
key :born_at, Time
key :active, Boolean
key :fav_colors, Array
end
The available options when defining keys
are:
Upvotes: 1