Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using PostgreSQL enums with ActiveRecord enums
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module EnumTest
class Application < Rails::Application
config.active_record.schema_format = :sql
end
end
class CreateWidgets < ActiveRecord::Migration[5.0]
def change
execute "CREATE TYPE widget_status AS ENUM ('ACTIVE', 'ARCHIVED')"
create_table :widgets do |t|
t.string :name
t.column :status, :widget_status, default: 'ACTIVE', null: false, index: true
t.timestamps
end
end
end
class Widget < ApplicationRecord
enum status: { active: 'ACTIVE', archived: 'ARCHIVED' }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment