PostgreSQL has the concept of enumerated types built into the database.
How would you implement a table with a column that uses an enumerated type in Rails 3? Do you need to define the enum in PostgreSQL somehow? How could you create a DB migration that does this?
Working in Rails 3.07, Ruby 1.92p180, PostgreSQL 8.3.
Rails does not support the
ENUMdatatype out of the box. This is because not all databases support it that datatype. I found that a common way of dealing withENUMvalues is to manually create the enum column in your database (PostgreSQL in your case), and deal with it as astringcolumn in your Rails application. Then, use the validates_inclusion_of validator to force the use of the allowed values.And use native SQL in your migration to add the enum field:
edit (June 2014)
Rails 4.1 now supports enums. The
validates_inclusion_ofcan now be changed to:(However, this is still not natively supported by the underlying database, so the native SQL migration is still needed.)