【问题标题】:named scope with where and where.not带有 where 和 where.not 的命名范围
【发布时间】:2015-02-17 18:48:31
【问题描述】:
scope :active_without_owner, -> { where(active: true, role: 'owner') }

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

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

我试过了

scope :active_without_owner, -> { where(active: true, not(role: 'owner')) }

还有许多其他组合...

【问题讨论】:

    标签: ruby-on-rails scope


    【解决方案1】:

    这将起作用:

    scope :active_without_owner, -> { where(active: true).where.not(role: 'owner')) }
    

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

    scope :active, -> { where(active: true) }
    scope :active_with_owner, -> { active.where(role: 'owner') }
    scope :active_without_owner, -> { active.where.not(role: 'owner')) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      相关资源
      最近更新 更多