I am trying to understand usage of validates_uniqeness_of.
Say I have, two column ‘name’ and ‘email’ in my table users
three unique key is possible for these two columns
- name and email together
My question is,
if I write
validates_uniqueness_of :name, :email
what uniqueness is it going to apply,
1 and 2
what is the syntax of validates_uniqeness_of to implement # 3
I had a similar question and what was replied to me was as follows:
You can do it by overriding the validate method on the model e.g.
errors.add(‘startlocation’, ‘must be different to endlocation’) if
startlocation == endlocation
which will get called on every save. You can also override
validate_on_create and validate_on_update to get finer grained control.
So, basically you’d want to see that if both the e-mail and the user
was unique you’d add an error outlined above. Now, the above code isn’t
exactly what you’ll do, but it at least gets you started in the right
Hope that helps,