Reputation: 6269
I want to avoid that somebody can create an user with the username 'Admin'. I use this username as Host to control some functions of my application. I know there is a expression to validate the uniqueness of an model record:
validates_uniqueness_of
But i only want that the username 'Admin' cant be created twice. I hope somebody has an idea! Thanks
Upvotes: 0
Views: 58
Reputation: 43875
You can use conditionals in your validation to do the following:
class Model
validates :username, uniqueness: true, if: :admin?
def admin?
username.downcase == 'admin'
end
end
With that said, they'd take away my developer card if I didn't discourage you from doing this.
Basing an admin account solely on whether the username is admin is setting yourself up for your security being compromised. Have a look at something like ActiveAdmin
for managing your administrator accounts so that they're at the very least separated from your user accounts.
Upvotes: 2
Reputation: 7869
Or just validates_uniqueness_of :username, if: -> {username == 'admin'}
in your model.
Upvotes: 1