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
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?