Latest changes in Rails 7 introduces new syntax for defining an enum.
Before
The previous syntax includes the key value pair for the enum name, its enum values, and the options.
After
The new syntax has made the enum name and the values a separate argument and the enum options can be passed as key value pairs.
These changes also work with the hash syntax.
The following instance methods will be generated by rails as usual and there are no changes here:
status_draft?
status_draft!
status_published?
status_published!
status_archived?
status_archived!
free_category?
free_category!
premium_category?
premium_category!
The following scopes will also be created
free_category
premium_category
Note that the scopes for status
won’t be created as we have provided scopes: false
.
Notice the changes in the option keys:
Old Option | New Option |
---|---|
_default |
default |
_prefix |
prefix |
_suffix |
suffix |
_scopes |
scopes |
Note: The previous syntax will keep working until it is deprecated.