Deprecate defining enums with keywords args#50987
Merged
rafaelfranca merged 1 commit intorails:mainfrom Feb 10, 2024
Merged
Conversation
nvasilevski
approved these changes
Feb 6, 2024
07f2368 to
9585337
Compare
Enums have historically been defined using keyword arguments:
```ruby
class Function > ApplicationRecord
enum color: [:red, :blue],
type: [:instance, :class],
_scopes: false
```
This has the advantage of being able to define multiple enums at once
with the same options. However, it also has a downside that enum options
must be prefixed with an underscore to separate them from the enum
definitions (to enable models to have enums with the same name as an
option).
In Rails 7, a new syntax was [introduced][1] to instead define enums with
positional arguments:
```ruby
class Function > ApplicationRecord
enum :color, [:red, :blue], scopes: false
enum :type, [:instance, :class], scopes: false
```
This new syntax eliminates the need to prefix options with an underscore,
and the docs were updated to recommend this new syntax.
However, both versions of the API have been supported since, and it has
started to cause some problems:
The first issue is that the available options have drifted. In Rails
7.1, an option was added to make assigning an invalid enum value use
validation errors instead of runtime errors. However, the equivalent
underscored prefix option was not added for the original enum syntax
Articles have been created that describe the new option in Rails 7.1,
but the examples in the articles use un-prefixed options with the old
syntax. This confusion has also lead to issues opened asking why that
incorrect syntax is not working.
Additionally, the presence of underscored options is just generally
confusing because it tends to imply an option is for internal use.
This commit aims to fix all of these issues by deprecating the old enum
syntax. With only one way to define enums, options cannot drift and
there will be less confusion around how enums should be defined.
[1]: 0618d2d
9585337 to
8c54251
Compare
yahonda
added a commit
to yahonda/rails
that referenced
this pull request
Feb 14, 2024
…lbox This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed` warning in Action Mailbox. * Steps to reproduce ```ruby git clone https://github.com/rails/rails cd rails/actionmailbox bundle install bin/test test/unit/router_test.rb ``` * Without this tommit ``` $ bin/test test/unit/router_test.rb ... snip .. DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3. Positional arguments should be used instead: enum :status, [:pending, :processing, :delivered, :failed, :bounced] (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31) Run options: --seed 65254 ............... Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s. 15 runs, 25 assertions, 0 failures, 0 errors, 0 skips $ ``` Follow up rails#50987 Refer to rails#51037
yahonda
added a commit
to yahonda/rails
that referenced
this pull request
Feb 14, 2024
…lbox This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed` warning in Action Mailbox. * Steps to reproduce ```ruby git clone https://github.com/rails/rails cd rails/actionmailbox bundle install bin/test test/unit/router_test.rb ``` * Without this commit ``` $ bin/test test/unit/router_test.rb ... snip .. DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3. Positional arguments should be used instead: enum :status, [:pending, :processing, :delivered, :failed, :bounced] (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31) Run options: --seed 65254 ............... Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s. 15 runs, 25 assertions, 0 failures, 0 errors, 0 skips $ ``` Follow up rails#50987 Refer to rails#51037
This was referenced Feb 14, 2024
hendrixfan
added a commit
to hendrixfan/mastodon
that referenced
this pull request
Feb 15, 2024
As of Rails 7.0, the enum syntax has been updated [1] to positional arguments without the prefix `_`. The old syntax is still supported [2], but it is recommended to use the new one. [1] rails/rails#41328 [2] rails/rails#50987 `
hendrixfan
added a commit
to hendrixfan/mastodon
that referenced
this pull request
Feb 15, 2024
As of Rails 7.0, the enum syntax has been updated [1] to positional arguments without the prefix `_`. The old syntax is still supported [2], but it is recommended to use the new one. [1] rails/rails#41328 [2] rails/rails#50987 `
hendrixfan
added a commit
to hendrixfan/mastodon
that referenced
this pull request
Feb 15, 2024
As of Rails 7.0, the enum syntax has been updated [1] to positional arguments without the prefix `_`. The old syntax is still supported [2], but it is recommended to use the new one. [1] rails/rails#41328 [2] rails/rails#50987 `
ghiculescu
added a commit
to ghiculescu/audits1984
that referenced
this pull request
Feb 19, 2024
Ridhwana
pushed a commit
to Ridhwana/rails
that referenced
this pull request
Mar 7, 2024
…lbox This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed` warning in Action Mailbox. * Steps to reproduce ```ruby git clone https://github.com/rails/rails cd rails/actionmailbox bundle install bin/test test/unit/router_test.rb ``` * Without this commit ``` $ bin/test test/unit/router_test.rb ... snip .. DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3. Positional arguments should be used instead: enum :status, [:pending, :processing, :delivered, :failed, :bounced] (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31) Run options: --seed 65254 ............... Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s. 15 runs, 25 assertions, 0 failures, 0 errors, 0 skips $ ``` Follow up rails#50987 Refer to rails#51037
rosa
pushed a commit
to basecamp/audits1984
that referenced
this pull request
Mar 21, 2024
viralpraxis
pushed a commit
to viralpraxis/rails
that referenced
this pull request
Mar 24, 2024
…lbox This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed` warning in Action Mailbox. * Steps to reproduce ```ruby git clone https://github.com/rails/rails cd rails/actionmailbox bundle install bin/test test/unit/router_test.rb ``` * Without this commit ``` $ bin/test test/unit/router_test.rb ... snip .. DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3. Positional arguments should be used instead: enum :status, [:pending, :processing, :delivered, :failed, :bounced] (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31) Run options: --seed 65254 ............... Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s. 15 runs, 25 assertions, 0 failures, 0 errors, 0 skips $ ``` Follow up rails#50987 Refer to rails#51037
fractaledmind
pushed a commit
to fractaledmind/rails
that referenced
this pull request
May 13, 2024
…lbox This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed` warning in Action Mailbox. * Steps to reproduce ```ruby git clone https://github.com/rails/rails cd rails/actionmailbox bundle install bin/test test/unit/router_test.rb ``` * Without this commit ``` $ bin/test test/unit/router_test.rb ... snip .. DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3. Positional arguments should be used instead: enum :status, [:pending, :processing, :delivered, :failed, :bounced] (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31) Run options: --seed 65254 ............... Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s. 15 runs, 25 assertions, 0 failures, 0 errors, 0 skips $ ``` Follow up rails#50987 Refer to rails#51037
2 tasks
yoones
pushed a commit
to yoones/rails
that referenced
this pull request
Mar 6, 2025
…lbox This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed` warning in Action Mailbox. * Steps to reproduce ```ruby git clone https://github.com/rails/rails cd rails/actionmailbox bundle install bin/test test/unit/router_test.rb ``` * Without this commit ``` $ bin/test test/unit/router_test.rb ... snip .. DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3. Positional arguments should be used instead: enum :status, [:pending, :processing, :delivered, :failed, :bounced] (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31) Run options: --seed 65254 ............... Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s. 15 runs, 25 assertions, 0 failures, 0 errors, 0 skips $ ``` Follow up rails#50987 Refer to rails#51037
sandbergja
added a commit
to pulibrary/bibdata
that referenced
this pull request
Mar 21, 2025
* enum has a slightly different API (see rails/rails#50987) * you now should specify a coder for serialized columns (see rails/rails#47463). Use YAML for backwards compatibility. * we weren't actually using one of the serialized columns, and it doesn't even exist in the database. Remove it from the model.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / Background
Enums have historically been defined using keyword arguments:
This has the advantage of being able to define multiple enums at once with the same options. However, it also has a downside that enum options must be prefixed with an underscore to separate them from the enum definitions (to enable models to have enums with the same name as an option).
In Rails 7, a new syntax was introduced to instead define enums with positional arguments:
This new syntax eliminates the need to prefix options with an underscore, and the docs were updated to recommend this new syntax.
However, both versions of the API have been supported since, and it has started to cause some problems:
The first issue is that the available options have drifted. In Rails 7.1, an option was added to make assigning an invalid enum value use validation errors instead of runtime errors. However, the equivalent underscored prefix option was not added for the original enum syntax
Articles have been created that describe the new option in Rails 7.1, but the examples in the articles use un-prefixed options with the old syntax. This confusion has also lead to issues opened asking why that incorrect syntax is not working.
Additionally, the presence of underscored options is just generally confusing because it tends to imply an option is for internal use.
Detail
This commit aims to fix all of these issues by deprecating the old enum syntax. With only one way to define enums, options cannot drift and there will be less confusion around how enums should be defined.
Additional information
Closes #50333
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]