3 class CreateUserRoles < ActiveRecord::Migration[4.2]
4 class User < ApplicationRecord
7 class UserRole < ApplicationRecord
11 create_enumeration :user_role_enum, %w[administrator moderator]
13 create_table :user_roles do |t|
14 t.column :user_id, :bigint, :null => false
15 t.column :role, :user_role_enum, :null => false
17 t.timestamps :null => true
20 User.where(:administrator => true).each do |user|
21 UserRole.create(:user_id => user.id, :role => "administrator")
24 remove_column :users, :administrator
26 add_foreign_key :user_roles, :users, :name => "user_roles_user_id_fkey"
30 add_column :users, :administrator, :boolean, :default => false, :null => false
32 UserRole.where(:role => "administrator").each do |role|
33 user = User.find(role.user_id)
34 user.administrator = true
38 drop_table :user_roles
39 drop_enumeration :user_role_enum