关闭包含在数组中的记录

时间:2014-04-08 09:37:41

标签: ruby-on-rails activerecord

如何解除返回的记录,这些记录是LIKE来自数组的记录。

目前我只能用字符串过滤我的记录,但我想传递一个字符串数组。可能吗?怎么样?

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

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

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

3 个答案:

答案 0 :(得分:3)

试试这个,

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

答案 1 :(得分:1)

如果您使用的是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

答案 2 :(得分:0)

试试这个

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