James McMahon
James McMahon

Reputation: 49639

Devise user model ignoring model validations on email

My Devise user model is ignoring the uniqueness check I placed on the model,

validates :email, presence: true, length: { maximum: 255 }, uniqueness: true

And is instead hitting the database and blowing up when it hits the unique constraint on the database,

Duplicate entry '[email protected]' for key 'index_users_on_email'

Any ideas how I make it respect the model level validation?

Upvotes: 2

Views: 180

Answers (1)

James McMahon
James McMahon

Reputation: 49639

Honestly have no idea what happened here, but I just tried it again and it worked, with 2 errors for the email addresses,

Email has already been taken

I then removed my explicit uniqueness check, and it that reduced it down to 1 error. So Devise must handle the unique check automatically and something must have been gotten screwed up with my local server. Devise also handles the presence check automatically as well.

Side note, you do however need to put an explicit validator on the email length, otherwise it blows up.

 validates_length_of :email, maximum: 255

Upvotes: 0

Related Questions