add_column :users, :role, :string
class User < ActiveRecord::Base
attr_accessible :email, :password, :remember_me, :username
devise :database_authenticatable, ................
validates_uniqueness_of :username, :email
before_create :setup_default_role_for_new_users
ROLES = %w[admin default banned]
private
def setup_default_role_for_new_users
if self.role.blank?
self.role = "default"
end
end
end
Is there a way I can specifically validate a name in my :role column? I would like to make my "admin" role unique so I can guranteed its not used again for security reasons.
If you only have the one admin role you could do something like this:
If things start to get more complex, you could explore using Single Table Inheritance to separate the logic for each type of user into a separate class.