---
title: "Rails 6 adds negative scopes on enum"
description:
  "Rails 6 adds default negative scopes for all defined enum on model"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-negative-scopes-on-enum"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-negative-scopes-on-enum.md"
---

# Rails 6 adds negative scopes on enum

Rails 6 adds default negative scopes for all defined enum on model

- Author: Abhay Nikam
- Published: March 6, 2019
- Categories: Rails 6, Rails

When an enum attribute is defined on a model, Rails adds some default scopes to
filter records based on values of enum on enum field.

Here is how enum scope can be used.

```ruby
class Post < ActiveRecord::Base
  enum status: %i[drafted active trashed]
end

Post.drafted # => where(status: :drafted)
Post.active  # => where(status: :active)

```

In Rails 6, negative scopes are added on the enum values.

As mentioned by DHH in the pull request,

> these negative scopes are convenient when you want to disallow access in
> controllers

Here is how they can be used.

```ruby
class Post < ActiveRecord::Base
  enum status: %i[drafted active trashed]
end

Post.not_drafted # => where.not(status: :drafted)
Post.not_active  # => where.not(status: :active)

```

Check out the [pull request](https://github.com/rails/rails/pull/35381) for more
details on this.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-negative-scopes-on-enum)
