PostgreSQL provides in-built support for enumerated types that Rails can then take advantage of. However, it is often a pain to make use of this feature since it’s tedious to create custom enum types in PostgreSQL via ActiveRecord migrations.
Before
The previous method of creating custom enum types is done by executing direct SQL statements.
Arguably this is not convenient in reality!
After
Often many Rails + PostgreSQL developers use structure.sql
instead of schema.rb
to make it easier to manage custom
enum types.
Fortunately,
Rails 7 adds support for custom enum types in PostgreSQL.
This change removes that friction by introducing create_enum
to add a new enum type
and t.enum
to add a column.
Enum types work well with
ActiveRecord::Enum
and
now it’s easier than ever to utilize them.
The enum definitions and enum columns get added to the schema.rb, so we can load them into a test database, and they’ll work correctly.
It is important to note that this is not compatible with any other database adapters. So if one decides to change the database at any point in the future, this would have to be a manual migration.