Forum: Ruby on Rails find and LIKE 'xxx%' clause

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tony (Guest)
on 2006-06-14 13:43
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.
Brian Hogan (Guest)
on 2006-06-14 14:42
(Received via mailing list)
I'd be interested to know that as welll, as your solution is the one I
always see (and use).
3aafbe7cb5c02d2a214166630afe90ac?d=identicon&s=25 Stephen B. (steveb)
on 2006-06-14 15:05
(Received via mailing list)
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
Ezra Zygmuntowicz (Guest)
on 2006-06-14 18:46
(Received via mailing list)
On Jun 14, 2006, at 6:02 AM, Stephen Bartholomew wrote:

> statements? (foo = 1 OR foo = 2) AND (bar = 3 OR bar = 4).
>
>
> Brian Hogan 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* < tony.moutaux@igbmc.u-strasbg.fr
>> <mailto:tony.moutaux@igbmc.u-strasbg.fr>> 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
Brian Hogan (Guest)
on 2006-06-14 20:24
(Received via mailing list)
Ezra:

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

Awesome!
3aafbe7cb5c02d2a214166630afe90ac?d=identicon&s=25 Stephen B. (steveb)
on 2006-06-14 22:03
(Received via mailing list)
> My ez_where plugin handles this in a nice way IMHO ;) For example:
 > MyModel.find_where(:all) { name =~ "%#{params[:name]%" }

Excellent :0)

Steve
Tony (Guest)
on 2006-06-15 10:23
Ezra Zygmuntowicz 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.
This topic is locked and can not be replied to.