# Validates\_uniqueness\_of and Multiple Columns

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715>\
**Category:** Rails\
**Created:** [September 27, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715 "2007-09-27T21:35:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/1 "2007-09-27T21:35:07Z")

</div>

Yep, a newbie 😉

I first\_name and last\_name in a table. I’d like to know how I can use  
validates\_uniqueness\_of to make sure the combination of the two only  
appears once in my database.

Thanks a ton.  
-Dan

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/2 "2007-09-27T22:16:03Z")

</div>

vuo will accept multiple attribute names but checks them individually,  
not as an AND. You’d have to roll your own validation method. Look  
into the ActiveRecord validation docs on how to do that.

mike

Daniel L. wrote:

> I first\_name and last\_name in a table. I’d like to know how I can use  
> validates\_uniqueness\_of to make sure the combination of the two only  
> appears once in my database.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/3 "2007-09-27T22:36:59Z")

</div>

Bummer, but thanks for the tip.

-Dan

Mike P. wrote:

> vuo will accept multiple attribute names but checks them individually,  
> not as an AND. You’d have to roll your own validation method. Look  
> into the ActiveRecord validation docs on how to do that.
> 
> mike
> 
> Daniel L. wrote:
> 
> > I first\_name and last\_name in a table. I’d like to know how I can use  
> > validates\_uniqueness\_of to make sure the combination of the two only  
> > appears once in my database.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/4 "2007-09-27T22:52:27Z")

</div>

Hey, that seems to do the trick.

Thanks!  
-Dan

Bob S. wrote:

> On 9/27/07, Daniel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Yep, a newbie 😉
> > 
> > I first\_name and last\_name in a table. I’d like to know how I can use  
> > validates\_uniqueness\_of to make sure the combination of the two only  
> > appears once in my database.
> 
> Use :scope
> 
> validates\_uniqueness\_of :first\_name, :scope =\> :last\_name
> 
> An index on last\_name would be appropriate as well if this table is  
> large.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/5 "2007-09-27T22:45:33Z")

</div>

On 9/27/07, Daniel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Yep, a newbie 😉
> 
> I first\_name and last\_name in a table. I’d like to know how I can use  
> validates\_uniqueness\_of to make sure the combination of the two only  
> appears once in my database.

Use :scope

validates\_uniqueness\_of :first\_name, :scope =\> :last\_name

An index on last\_name would be appropriate as well if this table is  
large.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/6 "2007-09-27T23:00:08Z")

</div>

One more question:

Is there a simple method of defining my own error message that’s  
associated with a failed validation?

The response from the failed validation should be something like “A  
person with this name already exists in the database.” rather than what  
it says now.

Thanks much. I’m really enjoying Rails. I had to spend way too much time  
generating validation rules in PHP not to mention the scaffolding  
feature. Looks fairly easy to modify the scaffold code too by creating  
static content.

-Dan

Bob S. wrote:

> On 9/27/07, Daniel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Yep, a newbie 😉
> > 
> > I first\_name and last\_name in a table. I’d like to know how I can use  
> > validates\_uniqueness\_of to make sure the combination of the two only  
> > appears once in my database.
> 
> Use :scope
> 
> validates\_uniqueness\_of :first\_name, :scope =\> :last\_name
> 
> An index on last\_name would be appropriate as well if this table is  
> large.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/7 "2007-09-27T23:09:14Z")

</div>

Use the :message parameter:

:scope =\> …, :message =\> ‘is already in our database. Please enter a  
unique value.’

mike

Daniel L. wrote:

> One more question:
> 
> Is there a simple method of defining my own error message that’s  
> associated with a failed validation?
> 
> The response from the failed validation should be something like “A  
> person with this name already exists in the database.” rather than what  
> it says now.
> 
> Thanks much. I’m really enjoying Rails. I had to spend way too much time  
> generating validation rules in PHP not to mention the scaffolding  
> feature. Looks fairly easy to modify the scaffold code too by creating  
> static content.

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/8 "2007-09-27T23:44:49Z")

</div>

On 9/27/07, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> validates\_uniqueness\_of :first\_name, :scope =\> :last\_name
> 
> An index on last\_name would be appropriate as well if this table is large.

I’ve never done this, but I think I would like to write a composed\_of  
object (FullName maybe?) and define some validation on that.

class User \< ActiveRecord::Base  
composed\_of :full\_name, :mapping =\> %w(first\_name last\_name)

def validate  
errors.add\_to\_base(“Name must be unique”) unless full\_name.unique?  
end  
end

I like how that expresses the intent so clearly.

Pat

---

<div class="post-metadata">

**Author:** ![liberte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@liberte](https://www.ruby-forum.com/u/liberte)\
**Post date:** [September 27, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-and-multiple-columns/115715/9 "2007-09-27T23:37:47Z")

</div>

On Sep 27, 2007, at 3:35 PM, Daniel L. wrote:

> Yep, a newbie 😉
> 
> I first\_name and last\_name in a table. I’d like to know how I can use  
> validates\_uniqueness\_of to make sure the combination of the two only  
> appears once in my database.
> 
> Thanks a ton.  
> -Dan

validates\_uniqueness\_of :first\_name, :scope =\> :last\_name

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
