# Validates\_uniqueness\_of - how did I get two users?

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-how-did-i-get-two-users/161676>\
**Category:** Rails\
**Created:** [February 28, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-did-i-get-two-users/161676 "2009-02-28T21:50:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [February 28, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-did-i-get-two-users/161676/1 "2009-02-28T21:50:23Z")

</div>

I have the following in my user model:

validates\_uniqueness\_of :login, :email, :case\_sensitive =\> false

yet somehow I ended up with two users with the same email. Does this  
line mean that the COMBINATION is unique (I didn’t think it did).

Should I have this instead:

validates\_uniqueness\_of :login, :case\_sensitive =\> false  
validates\_uniqueness\_of :email, :case\_sensitive =\> false

Any ideas?

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [February 28, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-did-i-get-two-users/161676/2 "2009-02-28T22:42:43Z")

</div>

No, it doesn’t mean that the combination is unique, it means that  
:login and :email are going to be unique.

There are two possible reasons, the user was added manually at the  
database (which is bad, as the database should have a unique index at  
both columns) or you got into a racing condition, where the two  
selects hit the database at the same time and thus both returned  
false.

Best thing to do is to improve you database schema by creating some  
unique indexes that reflect your validations.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [March 1, 2009, 1:40am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-did-i-get-two-users/161676/3 "2009-03-01T01:40:57Z")

</div>

The odd thing is that the users were created through the website two  
months apart!

On Feb 28, 10:41 pm, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [March 2, 2009, 1:09am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-how-did-i-get-two-users/161676/4 "2009-03-02T01:09:53Z")

</div>

In ActiveRecord::Base, the save method takes a flag called  
perform\_validation, which you can set to false. Check your controller  
code to make sure someone didn’t decide to skip the validations  
somewhere.
