Rails: How to find_by a field containing a certain string Rails: How to find_by a field containing a certain string ruby-on-rails ruby-on-rails

Rails: How to find_by a field containing a certain string


I think something like this should work:

Topic.where("name like ?", "%apple%")

To accomodate for your edit:

Topic.where("name like ?", "%#{@search}%")

Basic string interpolation, you're using the value of @search inside the string %%, so you @search = "apple" then you end up with %apple%


With PostgreSQL you can also use match operators:

Topic.where("name ~* ?", @search)


Looks like in Rails 3 you would want to use the where:

Topic.where("name ILIKE ?", "%apple%")