user3166642
user3166642

Reputation: 105

Querying Database to retrieve all the records containing a given string in an array of strings present in the record

I'm having an Active Record in my rails application in which each row of the table MyTable contains:

  1. stringArr --> an array of string
  2. id

Now I want to query my database to pull all the records in which a particular queryString is present in strinArr of that record.

I know we can do MyTable.where(xyz:someXYZ) to pull all the records with xyz value as someXYZ?

But how can I do this query?

Upvotes: 2

Views: 160

Answers (2)

David Ferd
David Ferd

Reputation: 57

You can use the sql like clause

MyTable.where("stringArr LIKE :keywords",{keywords: '%querystring%'})

Upvotes: 1

Amit Suroliya
Amit Suroliya

Reputation: 1535

To fetch all record, which contains queryString in an array of records, you can do -

MyTable.where("column_name LIKE ?","'%queryString%'"})

This will do exact query string search means return all data, which column_name contains queryString.

Upvotes: 1

Related Questions