Rails 7 Introduces New Syntax for Enums
By Abhishek Kanojia on February 4, 2024
Welcome to the Future of Rails Enums in Version 7!
Rails 7 introduces a breath of fresh air with revamped Enum syntax, promising a cleaner and more intuitive coding experience.
Old vs. New: A Quick Look
In the old days, defining Enums involved a key-value pair jumble, but not anymore! Rails 7 separates enum names and values, making your code more straightforward:
# Before
enum status: [ :draft, :published, :archived ], _prefix: true, _scopes: false
# After
enum :status, [ :draft, :published, :archived ], prefix: true, scopes: false
Hash Syntax Delight
But that’s not all! The changes seamlessly extend to hash syntax, giving you even more flexibility:
# Hash syntax
enum :status, { draft: 0, published: 1, archived: 2 }, prefix: true, scopes: false
But why bother the switch?
Based on this PR#41328 discussion and is quoted:
That is due to enum takes one hash argument only, which contains both enum definitions and reserved options. I propose new syntax for enum to avoid leading _ from reserved options, by allowing enum(attr_name, …, **options) more Attribute API like syntax.
So it was to avoid _
in reserved options for enum definition.
Does it mean the older enum is deprecated?
It is not yet deprecated, which means the older syntax is still valid as of Rails 7.1.3
class Post < ApplicationRecord
enum status: [:draft, :published]
end
** Please note that the new `enum` validation options as mentioned in the Article will result in error if you try to use it with old syntax.
class Post < ApplicationRecord
enum status: [:draft, :published], validate: true
end
post = Post.new
#
# => `raise_conflict_error`: You tried to define an enum named "status" on the model "Post", but this will generate a instance method "draft?", which is already defined by another enum. (ArgumentError)