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

Brad Ediger
Wed, 25 Oct 2006 03:05:23 -0000

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%"])


On Oct 24, 2006, at 9:57 PM, 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])
>
> 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!!!
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >