0

I'm looking for a way to find all entries in a database containing a specific string. I know it's possible to iterate over every entry and see if a string matches but with a growing database i'd rather not do that. I'm wondering if there is a Rails way of doing it. something like

Table.where("content containsWord ?", "string")

So a function find all records containing a specific string. Does anyone know a way to do this?

update

duplicate of Rails: How to find_by a field containing a certain string

Community
  • 1
  • 1
Mischa
  • 2,069
  • 4
  • 23
  • 32

1 Answers1

9

You can try this

search_term = "string"
tables = Table.where("tables.content LIKE ?", "%#{search_term}%")

or, if you want to ignore case sensitiveness, then do this

search_term = "string"
tables = Table.where("lower(content) LIKE lower(?)", "%#{search_term}%")

Hope this helps!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78