【问题标题】:Dismiss records which are included in an array关闭包含在数组中的记录
【发布时间】:2014-04-08 09:37:41
【问题描述】:

如何消除返回的记录,这些记录是数组中的LIKE

目前我只能通过字符串过滤我的记录,但我想传递一个字符串数组。是否可以?怎么做?

array = ['test', 'test1', 'test2']

我想要与以下查询类似的内容,因为name 可以包含一个包含多个单词的字符串。

Model.where.not('name like ?', "%caps%")

【问题讨论】:

  • 您使用的是哪个数据库?
  • 我正在使用 postgresql
  • 好的,我想这会对你有所帮助stackoverflow.com/questions/4307411/…
  • @Alex 任何答案对你有用吗?
  • 没有,因为name包含多个单词的字符串,这就是我需要使用LIKE的原因,我仍然没有对这个问题进行排序。

标签: ruby-on-rails activerecord


【解决方案1】:

试试这个,

   array = ['test', 'test1', 'test2']
   Model.where('name not in (?)', array)

【讨论】:

  • 它不起作用,这就是为什么我需要使用LIKE,因为在我的专栏中有一个包含多个单词的字符串。
【解决方案2】:

如果你使用的是 Rails 4,你可以这样做:

Model.where.not(name: ['test','test1','test2'])

如果您想与like 一起使用,那么您可以这样做

@model = Model.where('name like ?', "%caps%") #Retrieving all records like `caps`

@model1 = Model.where('name not in (?)', @model) #Excluding the records in `@model` and gives you the remaining

【讨论】:

    【解决方案3】:

    试试这个

    Model.where('name not like (?)', "%caps%")
    

    【讨论】:

      猜你喜欢
      • 2022-12-30
      • 1970-01-01
      • 2011-12-05
      • 2011-03-05
      • 2012-03-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      相关资源
      最近更新 更多