# Validates\_uniqueness\_of two fields

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-two-fields/95579>\
**Category:** Rails\
**Created:** [June 6, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-two-fields/95579 "2006-06-06T18:13:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 6, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-two-fields/95579/1 "2006-06-06T18:13:53Z")

</div>

Is it possible to do a validates\_uniqueness\_of :name, :zip but only in  
that context? I’d like to be able to have that zip used again, but not  
in conjunction with a name already with that zip.

Does that make sense?

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 6, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-two-fields/95579/2 "2006-06-06T20:39:47Z")

</div>

Guest wrote:

> Is it possible to do a validates\_uniqueness\_of :name, :zip but only in  
> that context? I’d like to be able to have that zip used again, but not  
> in conjunction with a name already with that zip.
> 
> Does that make sense?

I’m new to Rails, but I think validate\_uniqueness\_of :name, :scope=\>:zip  
should do what you want.

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 6, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-two-fields/95579/3 "2006-06-06T22:12:35Z")

</div>

Perfect. Thanks yo.

Eric N. wrote:

> Guest wrote:
> 
> > Is it possible to do a validates\_uniqueness\_of :name, :zip but only in  
> > that context? I’d like to be able to have that zip used again, but not  
> > in conjunction with a name already with that zip.
> > 
> > Does that make sense?
> 
> I’m new to Rails, but I think validate\_uniqueness\_of :name, :scope=\>:zip  
> should do what you want.

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 7, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-two-fields/95579/4 "2006-06-07T14:05:16Z")

</div>

Guest wrote:

> Is it possible to do a validates\_uniqueness\_of :name, :zip but only in  
> that context? I’d like to be able to have that zip used again, but not  
> in conjunction with a name already with that zip.
> 
> Does that make sense?

Do you mean that the zip needs to be unique for each name (i.e. each  
name can have only one of each zip), or the other way round. If it’s the  
former, try:  
validates\_uniqueness of :zip, :scope =\> :name
