How do I test a unique index?

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

On 10/12/06, Shauna [email protected] 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

“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://mephistoblog.com

Thanks, that worked perfectly.

Shauna

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs