Rails Rails 6 中的 enum 现在可以使用非 scopes

wootaw · March 08, 2019 · 1270 hits

以前我们在 model 中定义了 enum 属性:

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

Rails 会添加一些默认的 scopes,那么可以这么使用:

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

Rails6 中现在还会添加一些非 scopes,现在可以这么写了:

Post.not_drafted # => where.not(status: :drafted)
Post.not_active  # => where.not(status: :active)
No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.