class AddStatusConstraintToIncidents < ActiveRecord::Migration[6.1]
def change
add_check_constraint :incidents,
"status IN ('open','acknowledged','resolved')",
name: 'incidents_status_check'
end
end
class Incident < ApplicationRecord
enum status: {
open: 'open',
acknowledged: 'acknowledged',
resolved: 'resolved'
}, _prefix: true
end
Rails enums are nice, but the DB should enforce allowed values. Use a CHECK constraint (or native enum type) plus the Rails enum mapping. It prevents bad writes from console scripts and future migrations.