I want to declare different user roles on my site, and I was wondering what is the best thing to do in Rails? At the moment, I have two options:
OPTION 1:
I create a Users table and declare a single column in the row where I can store user role names (SuperAdmin, Admin, Coach, Player)
create_table "users", force: true do |t|
t.string "username"
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "role"
end
Inside the user class, I store the values as follows:
class User < ActiveRecord::Base
ROLES = %w[SuperAdmin, Admin, Player, Coach]
end
OPTION 2:
I create a separate table for roles only. Inside the Users table, I have an integer column to hold role_id:
create_table "users", force: true do |t|
t.string "username"
t.string "first_name"
t.string "last_name"
t.string "email"
t.integer "role_id"
end
create_table "roles", force: true do |t|
t.string "role_name"
end
class User < ActiveRecord::Base
belongs_to :role
end
class Role < ActiveRecord::Base
has_many :users
end
What would be better if we used search speed, added new roles and future services to consideration?