Created
May 8, 2012 03:05
-
-
Save woods/2632236 to your computer and use it in GitHub Desktop.
How to dynamically query enum values from Postgresql in Rails
This file contains hidden or 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
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 |
This file contains hidden or 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
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
Credit: http://archives.postgresql.org/pgsql-novice/2008-12/msg00051.php