Skip to content

Instantly share code, notes, and snippets.

@christiannelson
Last active September 6, 2016 22:52
Show Gist options
  • Save christiannelson/97eb4c7920b613bd9c1a057b259c1753 to your computer and use it in GitHub Desktop.
Save christiannelson/97eb4c7920b613bd9c1a057b259c1753 to your computer and use it in GitHub Desktop.
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