Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a LIKE % query in ActiveRecord?

I'm trying to find some items that contain a certain string. If I do:

MyModel.where("description LIKE ?",keyword)

it will generate a query for an exact match. I would like to make it generate a LIKE %keyword% query. How can I do that?

like image 354
Geo Avatar asked Apr 02 '11 09:04

Geo


2 Answers

like_keyword = "%#{keyword}%"    
MyModel.where("description LIKE ?", like_keyword)
like image 156
rubyprince Avatar answered Oct 28 '22 03:10

rubyprince


MyModel.where("description LIKE (?)", "%#{keyword}%")
like image 36
fl00r Avatar answered Oct 28 '22 03:10

fl00r