Last active
September 6, 2016 22:52
-
-
Save christiannelson/97eb4c7920b613bd9c1a057b259c1753 to your computer and use it in GitHub Desktop.
Using PostgreSQL enums with ActiveRecord enums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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