# Validate uniqueness of category model depending on user model

**URL:** <https://www.ruby-forum.com/t/validate-uniqueness-of-category-model-depending-on-user-model/160628>\
**Category:** Rails\
**Created:** [February 18, 2009, 9:29am UTC](https://www.ruby-forum.com/t/validate-uniqueness-of-category-model-depending-on-user-model/160628 "2009-02-18T09:29:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [February 18, 2009, 9:29am UTC](https://www.ruby-forum.com/t/validate-uniqueness-of-category-model-depending-on-user-model/160628/1 "2009-02-18T09:29:14Z")

</div>

How to make unique validation on category model that is belong to user  
model and the unique validation must depend on user model so for  
example every user may have their own unique category name but two or  
more user may have a same category name but they can’t have a same  
category name twice.

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [February 18, 2009, 9:33am UTC](https://www.ruby-forum.com/t/validate-uniqueness-of-category-model-depending-on-user-model/160628/2 "2009-02-18T09:33:56Z")

</div>

could you give an example to explain that a little bit more?

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [February 18, 2009, 9:38am UTC](https://www.ruby-forum.com/t/validate-uniqueness-of-category-model-depending-on-user-model/160628/3 "2009-02-18T09:38:39Z")

</div>

On Feb 18, 8:28 am, Geekyra [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How to make unique validation on category model that is belong to user  
> model and the unique validation must depend on user model so for  
> example every user may have their own unique category name but two or  
> more user may have a same category name but they can’t have a same  
> category name twice.

Use the :scope option on validates\_uniqueness\_of ?

Fred

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [February 19, 2009, 9:24am UTC](https://www.ruby-forum.com/t/validate-uniqueness-of-category-model-depending-on-user-model/160628/4 "2009-02-19T09:24:18Z")

</div>

For example : (note: change category into product for easier  
understanding)  
There are 2 user :  
“User 1” may have product name it “CAR”, “HOUSE”, “HOUSE@XXX”  
“User 2” may have product name it “CAR”, “CAR 2”, “HOUSE@YYY”,  
“HOUSE”, “WALLET”

The point is every user may have a same product but they need to  
maintain a user uniqueness product so “User 1” can’t have a new  
product name “CAR” cos it’s already have it on the first row product.

I will try to look forward for Frederick suggestion, see if  
using :scope solve the problem. Thanks for Frederick
