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?
:draft
) for the status column in your migration? something like:t.integer :status, default: 1
? You probably will have to do achange_column
equivalent of that. – Monotheism