# Validates\_uniqeness\_of

**URL:** <https://www.ruby-forum.com/t/validates-uniqeness-of/76005>\
**Category:** Rails\
**Created:** [March 2, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/validates-uniqeness-of/76005 "2006-03-02T23:59:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Mohammad\_K](https://www.ruby-forum.com/u/Mohammad_K)\
**Post date:** [March 2, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/validates-uniqeness-of/76005/1 "2006-03-02T23:59:58Z")

</div>

Hello,

I am trying to understand usage of validates\_uniqeness\_of.  
Say I have, two column ‘name’ and ‘email’ in my table users

three unique key is possible for these two columns

1. name
2. email
3. name and email together

My question is,  
if I write  
validates\_uniqueness\_of :name, :email  
what uniqueness is it going to apply,  
1 and 2  
or,  
3 ?

Thanks,  
Mohammad

---

<div class="post-metadata">

**Author:** ![Mohammad\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Mohammad\_K](https://www.ruby-forum.com/u/Mohammad_K)\
**Post date:** [March 3, 2006, 12:18am UTC](https://www.ruby-forum.com/t/validates-uniqeness-of/76005/2 "2006-03-03T00:18:04Z")

</div>

1 and 2

---

<div class="post-metadata">

**Author:** ![Mohammad\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Mohammad\_K](https://www.ruby-forum.com/u/Mohammad_K)\
**Post date:** [March 3, 2006, 12:24am UTC](https://www.ruby-forum.com/t/validates-uniqeness-of/76005/3 "2006-03-03T00:24:06Z")

</div>

what is the syntax of validates\_uniqeness\_of to implement # 3

---

<div class="post-metadata">

**Author:** ![Mohammad\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Mohammad\_K](https://www.ruby-forum.com/u/Mohammad_K)\
**Post date:** [March 3, 2006, 2:24am UTC](https://www.ruby-forum.com/t/validates-uniqeness-of/76005/4 "2006-03-03T02:24:49Z")

</div>

I had a similar question and what was replied to me was as follows:

You can do it by overriding the validate method on the model e.g.

def validate  
errors.add(‘startlocation’, ‘must be different to endlocation’) if  
startlocation == endlocation  
end

which will get called on every save. You can also override  
validate\_on\_create and validate\_on\_update to get finer grained control.

So, basically you’d want to see that if both the e-mail and the user  
name  
was unique you’d add an error outlined above. Now, the above code isn’t  
exactly what you’ll do, but it at least gets you started in the right  
direction.

Hope that helps,  
Ryan
