# Like clause with active record

**URL:** <https://www.ruby-forum.com/t/like-clause-with-active-record/116672>\
**Category:** Rails\
**Created:** [October 8, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/like-clause-with-active-record/116672 "2007-10-08T18:06:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [October 8, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/like-clause-with-active-record/116672/1 "2007-10-08T18:06:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [October 8, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/like-clause-with-active-record/116672/2 "2007-10-08T18:11:01Z")

</div>

You missed Customer.find(:all, :conditions =\> [“name LIKE ?”,  
“%#{some\_variable}%”])

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [October 8, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/like-clause-with-active-record/116672/3 "2007-10-08T18:21:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [October 8, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/like-clause-with-active-record/116672/4 "2007-10-08T18:25:44Z")

</div>

On 10/8/07, dodgyboz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [October 8, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/like-clause-with-active-record/116672/5 "2007-10-08T19:15:39Z")

</div>

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:

> > > direction?
> 
> >

–  
Sincerely,

William P.
