sts
sts

Reputation: 380

Mongomapper uniqueness

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

Answers (1)

Gagan Gami
Gagan Gami

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:

  • :required – Boolean that declares validate_presence_of
  • :unique – Boolean that declares validates_uniqueness_of
  • :numeric – Boolean that declares validates_numericality_of
  • :format – Regexp that is passed to validates_format_of
  • :in – Array that is passed to validates_inclusion_of
  • :not_in – Array that is passed to validates_exclusion_of
  • :length – Integer, Range, or Hash that is passed to validates_length_of

Upvotes: 1

Related Questions