【问题标题】:Scope based on many possible exclusions?范围基于许多可能的排除?
【发布时间】:2011-08-19 16:06:18
【问题描述】:

我如何创建一个基本上这样说的范围:

scope :filtered, where('status != one string, another string, yet another, yep, even more')

所以基本上说“状态不等于以下任何字符串。”

字符串列表会随着时间的推移而扩展。

注意,我使用的是 PostgreSQL。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 scope


    【解决方案1】:

    试试这个:(未测试)

    scope :filtered, where(["status <> ALL ( ARRAY[?, ?] )", 'string_1', 'string_2'])
    

    【讨论】:

    • 我正在使用 PostgreSQL,很抱歉没有澄清这一点。
    【解决方案2】:

    你可以用这个:

    scope :filtered, where(["status NOT IN (?)", ["string1", "string2", "string3"]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-19
      • 2021-04-06
      • 2014-08-10
      • 2019-04-18
      • 2019-01-31
      • 1970-01-01
      • 2019-07-22
      • 2015-11-29
      相关资源
      最近更新 更多