Search code examples
ruby-on-railsenumsrails-activerecord

rails - apply default value for enum field


I want to have a default value for my enum field to prevent it from being nil. I did the following:

# db/schema.rb
create_table "templates", force: :cascade do |t|
  t.integer  "status"
end

# app/models/template.rb
class Template < ActiveRecord::Base
  STATUSES = [:draft, :published]
  enum status: STATUSES
  after_initialize :init

  def init
    self.status ||= STATUSES.index(:draft)
  end
end

I get the expected results in my local environment. But not quite in heroku. I need it to be the default value draft after updating status to nil, but in this context it becomes nil and yet the published scope still includes the updated row.

$ heroku run rails console

> Template.published.pluck :id
=> [1, 2]

> Template.find(1).update(status:nil)
Template Load (4.4ms)  SELECT  "templates".* FROM "templates" WHERE "templates"."id" = $1 LIMIT 1  [["id", 1]]
Template Load (4.4ms)  SELECT  "templates".* FROM "templates" WHERE "templates"."id" = $1 LIMIT 1  [["id", 1]]
(1.7ms)  BEGIN
(1.7ms)  BEGIN
(1.1ms)  COMMIT
(1.1ms)  COMMIT
=> true

> Template.find(1).status
=> nil

> Template.published.pluck :id
=> [1, 2]

Is this the correct use case to use an enum? Is there a peculiarity with my heroku environment that I'm missing out?


Solution

  • You can set the default value from the database declaration.

    create_table :templates do |t|
      t.column :status, :integer, default: 0
    end
    

    And then, map the relationship as following

    class Template < ActiveRecord::Base
      enum status: { draft: 0, published: 1 }
    end