具有 where 和 where.not 的命名范围

Oli*_*yns 6 scope ruby-on-rails

scope :active_without_owner, -> { where(active: true, role: 'owner') }
Run Code Online (Sandbox Code Playgroud)

返回角色设置为“所有者”的活动用户。

我无法弄清楚返回具有“所有者”以外角色的活动用户的语法。

我试过了

scope :active_without_owner, -> { where(active: true, not(role: 'owner')) }
Run Code Online (Sandbox Code Playgroud)

以及许多其他组合......

Phi*_*rom 5

这将起作用:

scope :active_without_owner, -> { where(active: true).where.not(role: 'owner')) }
Run Code Online (Sandbox Code Playgroud)

我会稍微改变一下并这样做,以便您可以重用active

scope :active, -> { where(active: true) }
scope :active_with_owner, -> { active.where(role: 'owner') }
scope :active_without_owner, -> { active.where.not(role: 'owner')) }
Run Code Online (Sandbox Code Playgroud)