Integrating Enums for Workflow Feature in Rails
This guide will walk through the process of implementing an enum component in our Rails application to enable our approval workflow feature. Essentially will allow a `Post` to have stages.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

This guide will walk through the process of implementing an enum component in our Rails application to enable our approval workflow feature. Essentially will allow a Post to have stages.

Let's begin by briefly looking at a method called enum provided by the Rails framework. This method allows you to declare values that can be mapped to integers in the database.

Let's start by adding the status field to the database table posts with this command:

rails g migration add_status_to_posts status:integer

Now, open the migration and set the default value to 0.

class AddStatusToPosts < ActiveRecord::Migration
  def change
    add_column :posts, :status, :integer, default: 0
  end
end

Next, migrate the database with the command:

bundle exec rake db:migrate

That will update the table columns. If you open the schema file and check out you'll see the change:

# db/schema.rb

create_table "posts", force: :cascade do |t|
  t.date     "date"
  t.text     "rationale"
  t.datetime "created_at",             null: false
  t.datetime "updated_at",             null: false
  t.integer  "user_id"
  t.integer  "status",     default: 0
end

Next, open posts.rb, and update the file so that it works with the enum component:

# app/models/post.rb

class Post < ActiveRecord::Base
  enum status: { submitted: 0, approved: 1, rejected: 2 }
  belongs_to :user
  validates_presence_of :date, :rationale
end

Here, we are creating an enum called status that contains three values:
- approved
- rejected
- submitted

This should be all that we need for right now.

To test this out let's open the rails console in sandbox mode and create a post.

p = Post.create!(date: Date.today, rationale: "Anything")

As you'll notice in the console output, the default value for status is 0. Now, to change the status, all that we have to do is run the command:

p.approved!

If you check the variable p, the status is now "approved".

Do you see how easy it is? This showcases the power of the enum component.

It's also easy to make database queries. If you run the command:

Post.approved

It will return all the records that have the status "approved".

Now, let's clear all the values in our database, since all the existing records are going to have nil value for status. The command to clear all the data and run the seed file is:

bundle exec rake db:setup

To test this out let's open the rails console in the sandbox mode again to confirm that our enum is working. We can run commands such as:

  • Post.submitted.count
  • Post.approved.count
  • Post.last.approved!

Can you imagine how much code we'd need to write in order to get this working properly? The enum method is a powerful and efficient way to implement stages in a Rails model.

Resources