Is there a built-in method to validate that two fields in combination are unique? e.g. if my table is : ------------------------------ username | password ------------------------------ user1 | pass1 the following happens MyObject.new(username=>"user1",password=>"pass2") => ok MyObject.new(username=>"user2",password=>"pass1") => ok MyObject.new(username=>"user1",password=>"pass1") => not ok Thanks --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
I am interested in this too. Does anybody have any ideas? On Feb 20, 8:57 am, "macarthy" <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there a built-in method tovalidatethattwofieldsincombination > are unique? > > e.g. if my table is : > > ------------------------------ > username | password > ------------------------------ > user1 | pass1 > > the following happens > > MyObject.new(username=>"user1",password=>"pass2") => ok > MyObject.new(username=>"user2",password=>"pass1") => ok > MyObject.new(username=>"user1",password=>"pass1") => not ok > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
bandito
2007-Feb-21 01:12 UTC
Re: Validating the uniqueness of the combination of two fields
validates_uniqueness_of :username, :scope => :password http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000944 (this is an extra query. You could write a unique index and rescue the exception). On 2/20/07, macarthy <justin.maccarthy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Is there a built-in method to validate that two fields in combination > are unique? > > e.g. if my table is : > > ------------------------------ > username | password > ------------------------------ > user1 | pass1 > > > the following happens > > MyObject.new(username=>"user1",password=>"pass2") => ok > MyObject.new(username=>"user2",password=>"pass1") => ok > MyObject.new(username=>"user1",password=>"pass1") => not ok > > > Thanks > > > > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---