# Find and LIKE 'xxx%' clause

**URL:** <https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350>\
**Category:** Rails\
**Created:** [June 14, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350 "2006-06-14T13:43:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [June 14, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350/1 "2006-06-14T13:43:30Z")

</div>

Hi there !

I try to figure out how to do something like this in Rails :

SELECT \* FROM my\_table WHERE name LIKE ‘GOGONAM%’

My problem is the % and the quotes.  
From now, I do like this :

searched\_name = params[:name]+’%’  
user = MyTable.find(:all, :conditions =\> [“name LIKE ?”, searched\_name])

But is there a more gracefull way to do a LIKE find in ActiveRecord ?

Thanks.

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [June 14, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350/2 "2006-06-14T14:42:41Z")

</div>

I’d be interested to know that as welll, as your solution is the one I  
always see (and use).

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [June 14, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350/3 "2006-06-14T15:05:19Z")

</div>

As far as i know, there’s no other way of doing it - i’ve had a quick  
look through the activerecord source too.

It might be nice to see something like:

MyModel.find(:all, :like =\> [:name =\> term])

But i’d question the benefit of it. Things like this can easily get out  
of control. Like what if you want to have OR or AND in that like  
statement? What if you need to group the like statements? (foo = 1 OR  
foo = 2) AND (bar = 3 OR bar = 4).

The fact that active record is quite verbose with it’s sql does mean it  
maintains a great deal of flexibility without being over complicated -  
the slightly un-graceful result is a small price to pay.

Saying that, it might nice to see a plugin if anyone feels they have  
better way. :0)

Steve

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [June 14, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350/4 "2006-06-14T18:46:48Z")

</div>

On Jun 14, 2006, at 6:02 AM, Stephen B. wrote:

> statements? (foo = 1 OR foo = 2) AND (bar = 3 OR bar = 4).
> 
> Brian H. wrote:
> 
> > I’d be interested to know that as welll, as your solution is the  
> > one I always see (and use).  
> > On 6/14/06, _Tony_ \< removed\_email\_address@domain.invalid  
> > [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
> > Hi there !  
> > I try to figure out how to do something like this in Rails :  
> > SELECT \* FROM my\_table WHERE name LIKE ‘GOGONAM%’  
> > My problem is the % and the quotes.

```
My ez_where plugin handles this in a nice way IMHO ;) For example:

```

MyModel.find\_where(:all) { name =~ “%#{params[:name]%” }

Is the same as

MyModel.find(:all, :conditions =\> [“name LIKE ?”, “%#{params[:name]}%”]

```
I have a new release of the plugin that has many new features for

```

stuff like this that I will be releasing as soon as I get time to do  
a writeup of the new features. For now you can get it like this:

script/plugin install svn://rubyforge.org//var/svn/ez-where

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [June 14, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350/5 "2006-06-14T20:24:05Z")

</div>

Ezra:

I was waiting to hear from you… I thought that your plugin would  
handle it  
but I wasn’t sure.

Awesome!

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [June 14, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350/6 "2006-06-14T22:03:50Z")

</div>

> My ez\_where plugin handles this in a nice way IMHO 😉 For example:  
> MyModel.find\_where(:all) { name =~ “%#{params[:name]%” }

Excellent :0)

Steve

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [June 15, 2006, 10:23am UTC](https://www.ruby-forum.com/t/find-and-like-xxx-clause/63350/7 "2006-06-15T10:23:29Z")

</div>

Ezra Z. wrote:

> My ez\_where plugin handles this in a nice way IMHO 😉 For example:

Thanks for pointing it ! I’ll try it as soon as possible.
