June 2024
Intermediate to advanced
456 pages
11h 34m
English
Enumerated types or “enums” are a way to express a limited set of possible values. In a database context, they are similar to constraints in that they “constrain” the possible values. Enums also act as documentation since they enumerate all possible values a column can have.
When adding an enumerated type object, it becomes part of your database structure, meaning it’s dumped to the db/structure.sql file when migrations run.
In Active Record, an array of enum values can be used with an inclusion validation to ensure input data matches one of the types.
Let’s look at an example. Although not an enumerated type specifically, the VehicleStatus class in the Rideshare codebase limits status values to DRAFT ...
Read now
Unlock full access