Skip to content

Instantly share code, notes, and snippets.

@woods
Created May 8, 2012 03:05
Show Gist options
  • Save woods/2632236 to your computer and use it in GitHub Desktop.
Save woods/2632236 to your computer and use it in GitHub Desktop.
How to dynamically query enum values from Postgresql in Rails
sql = %{
select enumlabel
from pg_enum
where enumtypid = 'company_status'::regtype
order by oid
}
result = ActiveRecord::Base.connection.execute(sql)
result.each do |row|
puts row['enumlabel']
end
CREATE TYPE company_status AS ENUM (
'Client',
'Partner',
'Vendor',
'Other'
);
CREATE TABLE companies (
id integer NOT NULL,
name text NOT NULL,
status company_status DEFAULT 'Other'::company_status NOT NULL,
);
@woods
Copy link
Author

woods commented May 8, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment