# Uniqueness\_of on many columns

**URL:** <https://www.ruby-forum.com/t/uniqueness-of-on-many-columns/208417>\
**Category:** Rails\
**Created:** [June 17, 2011, 4:44pm UTC](https://www.ruby-forum.com/t/uniqueness-of-on-many-columns/208417 "2011-06-17T16:44:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 17, 2011, 4:44pm UTC](https://www.ruby-forum.com/t/uniqueness-of-on-many-columns/208417/1 "2011-06-17T16:44:26Z")

</div>

Greetings!

I’m trying to set up a validation to ensure that a record is unique  
across  
three columns; col\_one, col\_two, col\_three.

tried this:  
validates\_uniqueness\_of :col\_one, :scope=\>[:col\_two, :col\_three]

and it really doesn’t work at all like I had hoped.  
the docs says this should work

class TeacherSchedule \< ActiveRecord::Base  
validates\_uniqueness\_of :teacher\_id, :scope =\> [:semester\_id,  
:class\_id]  
end

Just want to know if there are any experiences out there to share  
before I write my own validation code.

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [June 20, 2011, 1:13am UTC](https://www.ruby-forum.com/t/uniqueness-of-on-many-columns/208417/2 "2011-06-20T01:13:49Z")

</div>

Tom A. \<tom@…\> writes:

> Greetings!I’m trying to set up a validation to ensure that a record is  
> unique across three columns; col\_one, col\_two, col\_three. tried  
> this:validates\_uniqueness\_of :col\_one, :scope=\>[:col\_two, :col\_three]and  
> it  
> really doesn’t work at all like I had hoped.the docs says this should  
> work  
> class TeacherSchedule \< ActiveRecord::Base  
> validates\_uniqueness\_of :teacher\_id, :scope =\> [:semester\_id, :class\_id]  
> end  
> Just want to know if there are any experiences out there to share before I  
> write my own validation code.

Composite keys are frowned upon. You might want to reconsider what you  
are  
attempting and introduce a few models to handle the relationship.

Of course, you could always create a customer validator. See the rails  
API.

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [June 24, 2011, 3:16pm UTC](https://www.ruby-forum.com/t/uniqueness-of-on-many-columns/208417/3 "2011-06-24T15:16:39Z")

</div>

Can you post your sample data here and if possible your model and  
controller  
code?

I have used it on several apps, and works just fine for me.

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [June 24, 2011, 2:12pm UTC](https://www.ruby-forum.com/t/uniqueness-of-on-many-columns/208417/4 "2011-06-24T14:12:56Z")

</div>

Yeah, but in this case I’m just regurgitation someone else’s data and I  
don’t want to pick up duplicate records.
