Reputation: 413
I have an enum like this defined in a model
enum status: { started: "started", passed: "passed", failed: "failed" }
I want to add value draft: "draft"
to it
But as far as I understand I have to run a migration somehow for it to be added to the database. How would I do it? Might be a stupid question, please bear with me, thanks.
:edit Thanks for the feedback
I also need to make the newly added enum values the default for all the new models. This will likely need a migration, but how would I generate it?
Upvotes: 0
Views: 1404
Reputation: 36860
You can add an enum value to the list.
enum status: { started: "started", passed: "passed", failed: "failed", draft: "draft" }
If the enum was an array, you'd have to be sure to add the new value to the end of the array only, otherwise records would have the wrong status.
As for making it the default for new records, I would do this in the model, not the database...
class MyModel < ApplicationRecord
before_save :initialize_status
private
def initialize_status
self.status ||= 'draft' if new_record?
end
end
Upvotes: 2