Default Enum in Rails 6


Default Enum in Rails 6

Rails 6 provides a way to define default enum value. Let’s see how it works with an example. For this example we are using Rails alpha.

Default enum in action

Using the Post model, with status as draft & published, and will be using draft as default will be something like:

  class Post < ApplicationRecord
    enum status: %i[draft published], _default: :draft
  end


On checking a new post’s status turns out to be a draft. In previous versions of Rails this turns out to be nil.


default-enum-in-rails


Pull Request

Recommended Posts

Ruby 2.7 adds Enumerable#tally
After introducing the Enumerable#tally method in Ruby 2.7-preview updates. #tally as the name suggests gives...
How to rename column name in Rails
Rails rename_column Rails provides a simple method rename_column which can be used in a migration....
How to use Enum in Rails
Enum in Rails “Enum” for short for the word “Enumerator” is widely used in different...