# Newbie question - using sql "Like '%...." in Find problem

**URL:** <https://www.ruby-forum.com/t/newbie-question-using-sql-like-in-find-problem/73543>\
**Category:** Rails\
**Created:** [October 25, 2006, 4:57am UTC](https://www.ruby-forum.com/t/newbie-question-using-sql-like-in-find-problem/73543 "2006-10-25T04:57:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 25, 2006, 4:57am UTC](https://www.ruby-forum.com/t/newbie-question-using-sql-like-in-find-problem/73543/1 "2006-10-25T04:57:01Z")

</div>

Hi all,

I’m trying to implement a simple search box:

@items = Item.find(:all, :conditions =\> [“description like %?% or text  
like %?%”, @search\_text, @search\_text])

The problem with this is rails automatically encloses the search term in  
single quotes, resulting in this MySQL error:

Mysql::Error: #42000You have an error in your SQL syntax; check the  
manual that corresponds to your MySQL server version for the right  
syntax to use near ‘%‘stuff’% or text like %‘stuff’%)’ at line 1: SELECT

- FROM items WHERE (description like %‘stuff’% or text like %‘stuff’%)

Is there a simple (not necessarily elegant) way to fix this?

Thanks a lot!!!

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 25, 2006, 5:05am UTC](https://www.ruby-forum.com/t/newbie-question-using-sql-like-in-find-problem/73543/2 "2006-10-25T05:05:24Z")

</div>

Alex wrote:

> Hi all,
> 
> I’m trying to implement a simple search box:
> 
> @items = Item.find(:all, :conditions =\> [“description like %?% or text  
> like %?%”, @search\_text, @search\_text])  
> Try:  
> @items = Item.find(:all, :conditions =\> [“description like ? or text  
> like ?”, “%#{@search\_text}%”, “%#{@search\_text}%”])

Note that putting the % operator in the beginning essentially forces a  
full table scan (unless you have some sort of full text indexing  
perhaps) and could result in very slow searches.

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 25, 2006, 5:05am UTC](https://www.ruby-forum.com/t/newbie-question-using-sql-like-in-find-problem/73543/3 "2006-10-25T05:05:55Z")

</div>

> @items = Item.find(:all, :conditions =\> [“description like %?% or text  
> like %?%”, @search\_text, @search\_text])

Try Item.find(:all, :conditions =\> [“description like ? or text like ?”,  
“%#{@search\_text}%”, “%#{@search\_text}%”])

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 25, 2006, 5:07am UTC](https://www.ruby-forum.com/t/newbie-question-using-sql-like-in-find-problem/73543/4 "2006-10-25T05:07:28Z")

</div>

Try putting the wildcards in the parameters instead of the query:

@items = Item.find(:all, :conditions =\> [“description like ? or text  
like ?”, “%#@search\_text%”, “%#@search\_text%”])

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 26, 2006, 4:30am UTC](https://www.ruby-forum.com/t/newbie-question-using-sql-like-in-find-problem/73543/5 "2006-10-26T04:30:19Z")

</div>

Thanks all, putting wildcards in the parameters worked like a charm!!!
