Skip to content

Instantly share code, notes, and snippets.

@binarymason
Forked from woods/example.rb
Created May 24, 2017 18:59
Show Gist options
  • Select an option

  • Save binarymason/5cecf33dbfd35d81733112e7ee1b0601 to your computer and use it in GitHub Desktop.

Select an option

Save binarymason/5cecf33dbfd35d81733112e7ee1b0601 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,
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment