# Validates\_uniqueness\_of on two columns?

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-on-two-columns/68994>\
**Category:** Rails\
**Created:** [September 17, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-two-columns/68994 "2006-09-17T23:18:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clayton\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@Clayton\_C](https://www.ruby-forum.com/u/Clayton_C)\
**Post date:** [September 17, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-two-columns/68994/1 "2006-09-17T23:18:29Z")

</div>

I want to make sure a combination of email and name are unique in a  
database  
for a newsletter signup application

What would be the railsy-ish way to do it?

here are a couple of ways I can think of accomplishing this

o Extend validation

o Make a unique primary key column in the database

Clayton C.

WinterMarket Networks  
Phone: (604) 417-3254  
Vancouver, B.C. Canada  
[http://www.wintermarket.net](http://www.wintermarket.net)  
MSN:removed\_email\_address@domain.invalid  
AIM:drfrogrx

---

<div class="post-metadata">

**Author:** ![Clayton\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@Clayton\_C](https://www.ruby-forum.com/u/Clayton_C)\
**Post date:** [September 17, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-two-columns/68994/2 "2006-09-17T23:30:31Z")

</div>

I’m not sure if writing a validate method is considered “extending  
validation”, but that’s the way I’d do it. And also a DB constraint;  
just in case. It protects the integrity of your data regardless of  
how it got there, since not all paths may be through your application.

On 9/17/06, Clayton C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Phone: (604) 417-3254

–  
When a man says he approves of something in principle, it means he  
hasn’t the slightest intention of putting it into practice. –  
Bismarck

---

<div class="post-metadata">

**Author:** ![Clayton\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@Clayton\_C](https://www.ruby-forum.com/u/Clayton_C)\
**Post date:** [September 17, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-on-two-columns/68994/3 "2006-09-17T23:52:34Z")

</div>

Clayton C. wrote the following on 17.09.2006 23:11 :

> I want to make sure a combination of email and name are unique in a  
> database for a newsletter signup application
> 
> What would be the railsy-ish way to do it?

validates\_uniqueness of :email, :scope =\> :name

if you want to validate uniqueness of a triplet or more

validates\_uniqueness of :first\_attr, :scope =\> [ :second\_attr,  
:third\_attr … ]

Lionel
