# Customize validates\_uniqueness\_of

**URL:** <https://www.ruby-forum.com/t/customize-validates-uniqueness-of/59020>\
**Category:** Rails\
**Created:** [April 27, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/customize-validates-uniqueness-of/59020 "2006-04-27T22:54:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 27, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/customize-validates-uniqueness-of/59020/1 "2006-04-27T22:54:39Z")

</div>

I need to modify the validates\_uniqueness\_of function so that it will  
check that the string is unique but only in its own category.

For example right now I have

validates\_uniqueness\_of :title

so let’s say I have the :title “Title 1” already in the database with  
the :category of “1”

then if i wanted to enter the :title “Title 1” in the database again BUT  
with a :category of “2” I need it to go through.

In other words it should only create an error if I try to enter :title  
“Title 1” AND :category “1”

Thanks in advance for any suggestions.

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 27, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/customize-validates-uniqueness-of/59020/2 "2006-04-27T23:05:28Z")

</div>

Rather than modifying that validation keyword, why not create your own?

[http://wiki.rubyonrails.com/rails/pages/HowToAddCustomValidationKeywords](http://wiki.rubyonrails.com/rails/pages/HowToAddCustomValidationKeywords)

This may help with what you want to do, I’m not sure on what you’d have  
to  
put in the newly created keyword, but hopefully it’ll get you on the  
right  
track.

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 27, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/customize-validates-uniqueness-of/59020/3 "2006-04-27T23:20:18Z")

</div>

Matt R. wrote:

> Rather than modifying that validation keyword, why not create your own?
> 
> [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToAddCustomValidationKeywords)
> 
> This may help with what you want to do, I’m not sure on what you’d have  
> to  
> put in the newly created keyword, but hopefully it’ll get you on the  
> right  
> track.

Thanks Matt,

I’ll take a look at that. I was just hoping there was a way to modify  
the existing validates function.

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 28, 2006, 12:09am UTC](https://www.ruby-forum.com/t/customize-validates-uniqueness-of/59020/4 "2006-04-28T00:09:46Z")

</div>

BB wrote:

> then if i wanted to enter the :title “Title 1” in the database again BUT  
> with a :category of “2” I need it to go through.
> 
> In other words it should only create an error if I try to enter :title  
> “Title 1” AND :category “1”
> 
> Thanks in advance for any suggestions.

validates\_uniqueness\_of :title, :scope =\> “category”

Cheers

Matthias Oesterle

---

<div class="post-metadata">

**Author:** ![BBBBBB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@BBBBBB](https://www.ruby-forum.com/u/BBBBBB)\
**Post date:** [April 28, 2006, 12:30am UTC](https://www.ruby-forum.com/t/customize-validates-uniqueness-of/59020/5 "2006-04-28T00:30:43Z")

</div>

matthibcn wrote:

> BB wrote:
> 
> > then if i wanted to enter the :title “Title 1” in the database again BUT  
> > with a :category of “2” I need it to go through.
> > 
> > In other words it should only create an error if I try to enter :title  
> > “Title 1” AND :category “1”
> > 
> > Thanks in advance for any suggestions.
> 
> validates\_uniqueness\_of :title, :scope =\> “category”
> 
> Cheers
> 
> Matthias Oesterle

Damn that’s cool. Thanks a lot.
