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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/12/06, Shauna <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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?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 Olson http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanks, that worked perfectly. Shauna -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---