Introduction to Enum Definition in Rails 8

Rails 8 introduced a new syntax for defining enums that enhances readability and aligns better with modern Ruby syntax. Unlike earlier versions, where you defined an enum using:

class User < ApplicationRecord
  enum status: { active: 0, inactive: 1, banned: 2 }
end

Rails 8 now allows you to define it in a more streamlined way:

class User < ApplicationRecord
  enum :status, { active: 0, inactive: 1, banned: 2 }
end

This small yet significant change makes it easier to distinguish attributes in a model. The colon (:) before the attribute name improves clarity, ensuring that the attribute is directly tied to the enum.

Let’s explore this new approach in detail, covering migrations, querying, validations, and Rails 8-specific changes.