1 class CreateUserRoles < ActiveRecord::Migration[4.2]
2 class User < ApplicationRecord
5 class UserRole < ApplicationRecord
9 create_enumeration :user_role_enum, %w[administrator moderator]
11 create_table :user_roles do |t|
12 t.column :user_id, :bigint, :null => false
13 t.column :role, :user_role_enum, :null => false
15 t.timestamps :null => true
18 User.where(:administrator => true).each do |user|
19 UserRole.create(:user_id => user.id, :role => "administrator")
22 remove_column :users, :administrator
24 add_foreign_key :user_roles, :users, :name => "user_roles_user_id_fkey"
28 add_column :users, :administrator, :boolean, :default => false, :null => false
30 UserRole.where(:role => "administrator").each do |role|
31 user = User.find(role.user_id)
32 user.administrator = true
36 drop_table :user_roles
37 drop_enumeration :user_role_enum