John
John

Reputation: 634

Dismiss records which are included in an array

How can I dismiss the returned records which are LIKE those from an array.

At the moment I can filter my records only by a string, but I would like to pass an array of strings. Is it possible? And how?

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

I would like something similar with the below query because name can contain a string with multiple words.

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

Upvotes: 0

Views: 55

Answers (3)

Pavan
Pavan

Reputation: 33552

If you are using Rails 4,you can do like this:

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

If you want use with like then you have do like this

@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

Upvotes: 1

Jenorish
Jenorish

Reputation: 1714

Try this

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

Upvotes: 0

rajesh023
rajesh023

Reputation: 714

Try this,

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

Upvotes: 3

Related Questions