[Rails] :conditions => ["phone like '%:phone%'" , {:phone => "555"}]

dblack at wobblini.net dblack at wobblini.net
Mon Mar 20 13:53:56 GMT 2006


Hi --

On Mon, 20 Mar 2006, Alain Ravet wrote:

> Hi all,
>
>
> What's wrong with the way I specify the condition in :
>
>     Person.find :all, :conditions => ["phone like '%:phone%'" , {:phone
> => "555"}]
> ?
>
> It translates to (note the two ' between the two ")
>    SELECT * FROM people WHERE (phone like "%'555'%" )
>
> instead of
>    SELECT * FROM people WHERE (phone like "%555%" )
>
>
> Another way to produce this problem:
>
> OK :
> 	model = "555"
>        conditions =  "value like '%#{model}%'"
>
> ERROR :
> 	model = "555"
>        conditions =  ["value like '%?%'",model]    ==>  WHERE (phone
> like "%'555'%" )
>
> What's the write way to write this query?

The way that works :-)  Actually you can do this:

   "value like ?", "%#{model}%"

or equivalent, but I'd rather do "value like '%#{model}%'" in the
first place (unless there's an advantage to the ? technique that I'm
not taking into account).


David

-- 
David A. Black (dblack at wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


More information about the Rails mailing list