Skip to main content Link Search Menu Expand Document (external link)

Admin Account

In this article we will make two different genre for our user, namely client and admin. Admin is a role that frequently has the most access to and maintains a website. The Client, as the name suggests, is the client or user of our website.

Table Of Contents

  1. Enum
  2. Equality check with Enum
  3. Updating with Enum
  4. Scope with Enum

Enum

An enum is a kind of attribute whose values correspond to integers in the database and may be queried by name.

We could define an enum for our genre attribute, where the values are client and admin. Let’s begin by adding genre column to our users table. Run your projects container and generate a migration file.

 root@0122:/usr/src/app# rails g migration add_genre_to_users
      invoke  active_record
      create    db/migrate/xxxxxxxxxxxxxx_add_genre_to_users.rb

The datatype of genre will be integer with default value 0.

# db/migrate/xxxxxxxxxxxxxx_add_genre_to_users.rb

class AddGenreToUsers < ActiveRecord::Migration[7.0]
  def change
+   add_column :users, :genre, :integer, default: 0
  end
end

Then run rails db:migrate.

 root@0122:/usr/src/app#  rails db:migrate
== xxxxxxxxxxxxxx AddGenreToUsers: migrating ============
-- add_column(:users, :genre, :integer, {:default=>0})
   -> 0.0108s
== xxxxxxxxxxxxxx AddGenreToUsers: migrated (0.0112s) ===

After migrating, setup the enum to our user model.

# app/models/user.rb

class User < ApplicationRecord
  # ...
  has_many :comments

+ enum genre: { client: 0, admin: 1 }
end

This kind of enum declaration maps the relation between genre attribute and database integer with a hash.

Equality check with Enum

To determine if a user is client, we may use user.genre == 'client' or the enum helper user.client?.

Open your rails console and test out the enum helper. Since we set the default value of genre as 0, all users genre should be client.

irb(main):001:0> user = User.first
irb(main):002:0> user.genre == 'client'   # => true
irb(main):003:0> user.client?             # => true
irb(main):004:0> user.admin?              # => false

Updating with Enum

In addition, Ruby on Rails includes a helper for updating the enum value. Instead of user.update(genre: :admin), we can use user.admin!. Now let’s try updating a users genre to admin.

irb(main):001:0> user = User.first
irb(main):002:0> user.admin!
  TRANSACTION (0.4ms)  BEGIN
  User Update (0.4ms)  UPDATE `users` SET `users`.`updated_at` = 'xxxx-xx-xx xx:xx:xx.xxxxxx', `users`.`genre` = 1 WHERE `users`.`id` = 1
  TRANSACTION (0.7ms)  COMMIT
=> true

Scope with Enum

It also generates scopes automatically. The standard method would be to use a where query, such as User.where(genre: :client). This query may be translated to User.client using the enum helper.

Now we successfully made an admin account using enum. To know more about enum, please read ActiveRecord Enum.


Back to top

Copyright © 2020-2022 Secure Smarter Service, Inc. This site is powered by KodaCamp.