Validating the uniqueness of the combination of two fields

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

I am interested in this too. Does anybody have any ideas?

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).

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