# How can I implement uniqueness?

**URL:** <https://www.ruby-forum.com/t/how-can-i-implement-uniqueness/109317>\
**Category:** Rails\
**Created:** [July 13, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/how-can-i-implement-uniqueness/109317 "2007-07-13T12:02:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jojojobbins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jojojobbins](https://www.ruby-forum.com/u/jojojobbins)\
**Post date:** [July 13, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/how-can-i-implement-uniqueness/109317/1 "2007-07-13T12:02:32Z")

</div>

Hello guys,

Suppose I have 2 models: Country and State.

The only field they have is called “name”. And I want this field to be  
unique in Country, and unique in State. But I want the uniqueness of the  
State’s name to be only within his Country.

So if I have a Country called “Mexico”, I shouldn’t be able to have to  
States with the same name in there.  
But I should be able to have one State called “Roca” in a Country called  
“Japan”, and another State called “Roca” in a different Country without  
problems.

How can I achieve this?

---

<div class="post-metadata">

**Author:** ![jojojobbins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jojojobbins](https://www.ruby-forum.com/u/jojojobbins)\
**Post date:** [July 13, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/how-can-i-implement-uniqueness/109317/2 "2007-07-13T12:17:24Z")

</div>

Put this in state  
validates\_uniqueness\_of :name,:scope=\>country\_id

Khurram Ijaz  
[http://www.renai-soft.com](http://www.renai-soft.com)

Adrian De la cruz wrote:

> Hello guys,
> 
> Suppose I have 2 models: Country and State.
> 
> The only field they have is called “name”. And I want this field to be  
> unique in Country, and unique in State. But I want the uniqueness of the  
> State’s name to be only within his Country.
> 
> So if I have a Country called “Mexico”, I shouldn’t be able to have to  
> States with the same name in there.  
> But I should be able to have one State called “Roca” in a Country called  
> “Japan”, and another State called “Roca” in a different Country without  
> problems.
> 
> How can I achieve this?

---

<div class="post-metadata">

**Author:** ![jojojobbins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jojojobbins](https://www.ruby-forum.com/u/jojojobbins)\
**Post date:** [July 13, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/how-can-i-implement-uniqueness/109317/3 "2007-07-13T12:20:37Z")

</div>

Khurram Ijaz wrote:

> Put this in state  
> validates\_uniqueness\_of :name,:scope=\>country\_id
> 
> Khurram Ijaz  
> [http://www.renai-soft.com](http://www.renai-soft.com)

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![jojojobbins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jojojobbins](https://www.ruby-forum.com/u/jojojobbins)\
**Post date:** [July 13, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/how-can-i-implement-uniqueness/109317/4 "2007-07-13T14:06:57Z")

</div>

Adrian De la cruz wrote:

> Khurram Ijaz wrote:
> 
> > Put this in state  
> > validates\_uniqueness\_of :name,:scope=\>country\_id
> > 
> > Khurram Ijaz  
> > [http://www.renai-soft.com](http://www.renai-soft.com)
> 
> Thanks a lot!

If you want to ensure this is the case  
add a migration

add\_index :states, [:country\_id, :name], :unique =\> true

that’s raise a MySQL error if you try and break the constraint.
