# Validates\_uniqueness\_of with conditions

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-with-conditions/119608>\
**Category:** Rails\
**Created:** [November 8, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-with-conditions/119608 "2007-11-08T13:37:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [November 8, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-with-conditions/119608/1 "2007-11-08T13:37:18Z")

</div>

Hello

I have a user table in my db.  
A user has a status field, status is an INT an can be between 1-5.

When a new user sign up I want to use “validates\_uniqueness\_of :login”  
in user model.  
But I don’t want to validate the new user against users with status ==  
5.

Can I do this with some :if or :conditions?

(I prefer not to use my own validate method)

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [November 8, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-with-conditions/119608/2 "2007-11-08T15:28:42Z")

</div>

On Nov 8, 2007 7:37 AM, Daniel Hansson  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can I do this with some :if or :conditions?
> 
> (I prefer not to use my own validate method)

I don’t think it can be done. I see two options:

- Write your own validate method
- Create another column in the table that would have the a single  
value for all rows with status 1-4 and use that in the :scope option  
of validates\_uniqueness\_of. You could manage this column automatically  
via before\_save.
