# How do I test a unique index?

**URL:** <https://www.ruby-forum.com/t/how-do-i-test-a-unique-index/71904>\
**Category:** Rails\
**Created:** [October 12, 2006, 8:32am UTC](https://www.ruby-forum.com/t/how-do-i-test-a-unique-index/71904 "2006-10-12T08:32:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 12, 2006, 8:32am UTC](https://www.ruby-forum.com/t/how-do-i-test-a-unique-index/71904/1 "2006-10-12T08:32:06Z")

</div>

I have a loans table and a payments table. Each loan has\_many payments.  
So far no problem.

Now, the payments table has a payment\_number field, and each payment has  
a payment\_number unique within that loan. For example, Loan A can have  
payment\_numbers 0, 1, and 2 and Loan B can have payment\_numbers 0, 1, 2,  
and 3 (but only one of each payment\_number). I can enforce this with a  
unique index in the payments table on the combination of loan\_id and  
payment\_number.

How do I enforce this in my model? When I run a unit test with just the  
index set I get a “Mysql::Error: #23000Duplicate entry…”. I would like  
to have something like a validates\_uniqueness\_of on the combination of  
loan\_id and payment\_number. How do I do that?

Thanks,  
Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 12, 2006, 9:32am UTC](https://www.ruby-forum.com/t/how-do-i-test-a-unique-index/71904/2 "2006-10-12T09:32:42Z")

</div>

On 10/12/06, Shauna [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do I enforce this in my model? When I run a unit test with just the  
> index set I get a “Mysql::Error: #23000Duplicate entry…”. I would like  
> to have something like a validates\_uniqueness\_of on the combination of  
> loan\_id and payment\_number. How do I do that?

[http://rails.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000816](http://rails.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000816)

“It can also validate whether the value of the specified attributes  
are unique based on multiple scope parameters. For example, making  
sure that a teacher can only be on the schedule once per semester for  
a particular class.”

validates\_uniqueness\_of :payment\_number, :scope =\> :loan\_id

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 13, 2006, 12:10am UTC](https://www.ruby-forum.com/t/how-do-i-test-a-unique-index/71904/3 "2006-10-13T00:10:03Z")

</div>

Thanks, that worked perfectly.

Shauna
