Default Enum in Rails 6
By Abhishek Kanojia on August 2, 2020
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
.