"not"-scopes in ActiveRecord::Enum
ActiveRecord::Enum
got a nice new feature in Rails 6.
class User < ApplicationRecord
enum status: [:active, :inactive, :radioactive]
end
The Model automatically receives these methods
User.active # where(status: 0)
User.not_active # where.not(status: 0)
User.inactive # where(status: 1)
User.not_inactive # where.not(status: 1)
User.radioactive # where(status: 2)
User.not_radioactive # where.not(status: 2)
The Model’s objects get these methods (unchanged since Rails 5)
user.active? # status == 0
user.active! # update!(status: 0)
user.inactive? # status == 1
user.inactive! # update!(status: 1)
user.radioactive? # status == 2
user.radioactive! # update!(status: 2)
https://api.rubyonrails.org/v6.0.0/classes/ActiveRecord/Enum.html