rails - apply default value for enum field
<p>I want to have a default value for my enum field to prevent it from being nil. I did the following:</p>

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

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

  def init
    self.status ||= STATUSES.index(:draft)
  end
end
</code></pre>

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

<pre><code>$ heroku run rails console

&gt; Template.published.pluck :id
=&gt; [1, 2]

&gt; 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
=&gt; true

&gt; Template.find(1).status
=&gt; nil

&gt; Template.published.pluck :id
=&gt; [1, 2]
</code></pre>

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