Forum: Ruby on Rails validates_uniqeness_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8c786b59665b31d9e43da6c5e6315a4d?d=identicon&s=25 Mohammad Khan (Guest)
on 2006-03-02 23:59
(Received via mailing list)
Hello,

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
1. name
2. email
3. 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
or,
3 ?

Thanks,
Mohammad
720fde074c77f4c9692dd6f05802d581?d=identicon&s=25 Nithin Reddy (jashugan)
on 2006-03-03 00:18
(Received via mailing list)
1 and 2
8c786b59665b31d9e43da6c5e6315a4d?d=identicon&s=25 Mohammad Khan (Guest)
on 2006-03-03 00:24
(Received via mailing list)
what is the syntax of validates_uniqeness_of to implement # 3
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-03-03 02:24
(Received via mailing list)
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.

def validate
 errors.add('startlocation', 'must be different to endlocation') if
startlocation == endlocation
end

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
name
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
direction.

Hope that helps,
Ryan
This topic is locked and can not be replied to.