Rails Devise - admin role, model vs attribute

I know how to create an administrator / user role: https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role

However, I am wondering if there are any advantages or disadvantages of the two options that should be considered when choosing between them. Can anyone tell about this please?

+6
source share
2 answers

Let me confuse the water a bit. I prefer this through the Role table and the UserRole connection UserRole . This way I can define more than one role without adding another column / table in db.

 class User has_many :user_roles has_many :roles, :through => :user_roles def role?(role) role_names.include? role end def role_names @role_names ||= self.roles.map(&:name) end def role=(role) self.roles << Role.find_or_create_by_name(role) end end class UserRole # integer: user_id, integer: role_id belongs_to :user belongs_to :role end class Role # string: name has_many :user_roles has_many :users, :through => :user_roles end 
+6
source

It depends on what you want to do with the admin role. The first option, I would say, is a bit safe, since the administrator role is a unique model in itself.

The second option is simple and will help you with minimal effort. However, if your users calculate a logical variable and how to set it, any user can become an administrator and access areas that you do not want.

+4
source

Source: https://habr.com/ru/post/957328/


All Articles