Rails 8.2 Fixes Enum Negative Scopes to Include Records with NULL Values
A closer look at how Rails 8.2 improves enum behavior by including NULL records in negative scopes. Learn why this change matters and what it means for everyday queries.

Chaitali Khangar
Technical Architect

Working with enums in Rails is usually straightforward. They provide clear value by generating predictable scopes and improving the readability of model queries.
However, certain enum behaviors in previous Rails versions created inconsistencies, particularly around how negative scopes handled NULL values. This often led to results that didn’t fully match developer expectations in real-world scenarios.
But the long-awaited solution to this problem is now available.
Rails 8.2 introduced an important update that resolves this issue and ensures that enum “negative” scopes such as not_published or not_active, correctly include records where the enum column is NULL.
This change was introduced in PR #55912 and improves the consistency of Active Record queries when using Rails enums.
Before
When you define an enum, Rails automatically generates both positive and negative scopes:
class Article < ApplicationRecord
enum status: { draft: 0, published: 1 }
end
You can then query using:
Article.published # => status = 1
Article.not_published # => status != 1
However, before this update, negative scopes would exclude records where status was NULL.
For example:
Article.create!(status: nil)
Article.not_published
# => [] (unexpectedly empty)
Even though the record clearly isn’t published, it wouldn’t appear, because SQL’s != comparison doesn’t match NULL.
Developers often had to work around this by manually adding conditions like:
Article.where.not(status: :published).or(Article.where(status: nil))
After
Rails now includes records with NULL values when using negative enum scopes.
The same query now behaves as expected:
Article.not_published
# => returns all articles where status is not 'published', including NULLs
This makes enum queries more intuitive and aligns them with real-world expectations:
when you say “not published,” you usually mean “everything that isn’t published”, even if the status hasn’t been set yet.
Reference: Pull Request #55912
In short, Rails now includes NULL records in enum negative scopes, ensuring queries like not_published return complete and accurate results.
This Rails 8.2 update may seem small on the surface, but it removes a long-standing friction point for developers who rely on enums for state management. With more predictable behavior and clearer query results, teams can now depend on negative enum scopes without adding custom conditions or workarounds.
Our team of Rails experts at RailsFactory views this as a meaningful improvement that strengthens the reliability of Rails 8 for everyday application logic.
Way to go, Rails!



