Like clause with active record

I am trying to use a like clause with my rails app but am having some problems. I have tried

customers = Customer.find(:all, :conditions => ["name LIKE ?", %some_variable%])

i have also tried

customers = Customer.find_by_sql("where name LIKE '%some_variable%')

and

customers = Customer.find_by_sql("SELECT * FROM customers WHERE name LIKE 'some_variable'")

The top 2 queries return results but not the results i would expect if some_variable is james i get about 100 rows returned with all sorts of names not just james

Can anyone help suggest what i'm doing wrong or point me in the right direction?

You missed Customer.find(:all, :conditions => ["name LIKE ?", "%#{some_variable}%"])

I think you'll find that you only need to use #{variable} when the variable is inside quotations. This problem is not one of using the correct rails syntax in this instance but thanks for the reply

No, andrewbruce gave you the proper solution. Look closely at what he showed you.

When using the LIKE modifier in Mysql, the % matches one or more characters and the _ match a single character. Hence the “%#{some_variable}%” would match anything containing the value of some_variable.

-Bill

dodgyboz wrote: