# Case insensitive search

**URL:** <https://www.ruby-forum.com/t/case-insensitive-search/79435>\
**Category:** Rails\
**Created:** [March 10, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/case-insensitive-search/79435 "2006-03-10T13:56:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [March 10, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/case-insensitive-search/79435/1 "2006-03-10T13:56:28Z")

</div>

I am having trouble with a simple gallery search.  
I type in a segment of the address and i only  
seem to be getting results if I use the correct case.

This is in my Gallery controller:

def search  
@gallery = Gallery.find(:all, :include =\> :property,  
:conditions =\> “address LIKE ‘%#{@params[:keywords]}%’”)  
end

On a different note:  
I am having trouble trying to access Gallery.property even though my  
Model  
associations have been setup. ie:  
Gallery belongs\_to Property (foreign key -\> property\_id)  
Property has\_many Galleries

---

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [March 12, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/case-insensitive-search/79435/2 "2006-03-12T22:13:19Z")

</div>

On 3/10/06, bmgz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am having trouble with a simple gallery search.  
> I type in a segment of the address and i only  
> seem to be getting results if I use the correct case.
> 
> This is in my Gallery controller:
> 
> def search  
> @gallery = Gallery.find(:all, :include =\> :property,  
> :conditions =\> “address LIKE ‘%#{@params[:keywords]}%’”)  
> end

Hi,

If you’re using MySQL, that should be working fine. Under postgres,  
you’d need to use “ilike” rather than “like” for a case insensitive  
search.

Not sure about other databases and what they use for case insensitive  
searches.

Rick

---

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [March 12, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/case-insensitive-search/79435/3 "2006-03-12T22:38:05Z")

</div>

On Sun, 2006-03-12 at 13:10 -0800, Rick T. wrote:

> > end
> 
> Hi,
> 
> If you’re using MySQL, that should be working fine. Under postgres,  
> you’d need to use “ilike” rather than “like” for a case insensitive  
> search.
> 
> Not sure about other databases and what they use for case insensitive searches.

* * *

seems to me that you abstract it by using (params[:keywords]).downcase  
and then you can do it ruby way instead of sql specific commands for one  
db or another but I’m not too certain about the usage above…  
#{@params[:keywords]} which just looks wrong and I would think that you  
have to do ‘%’ + YOUR\_SEARCH\_PHRASE + ‘%’ anyway…so it seems that  
there are a lot of things wrong with OP’s question.

Craig

---

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [March 13, 2006, 8:27am UTC](https://www.ruby-forum.com/t/case-insensitive-search/79435/4 "2006-03-13T08:27:45Z")

</div>

Craig W. wrote:  
…

> seems to me that you abstract it by using (params[:keywords]).downcase  
> and then you can do it ruby way instead of sql specific commands for one  
> db or another but I’m not too certain about the usage above…

The problem isn’t with the params[:keywords] being case sensitive but  
with  
the database itself (Postgres). The previous poster sorted this out by  
mentioning ILIKE. Nothing wring with choosing a database and actually  
USING  
it’s specific functions I supoose…

> #{@params[:keywords]} which just looks wrong and I would think that you

Explain? it works perfectly well. “you can execute ruby code in a string  
by using #{}” which is much neater than “doing” + this + “when” + you +  
"have alot of " + stuff\_going\_on

---

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [March 13, 2006, 8:52am UTC](https://www.ruby-forum.com/t/case-insensitive-search/79435/5 "2006-03-13T08:52:09Z")

</div>

On Mon, 2006-03-13 at 09:24 +0200, Brendon G. wrote:

> Craig W. wrote:  
> …
> 
> > seems to me that you abstract it by using (params[:keywords]).downcase  
> > and then you can do it ruby way instead of sql specific commands for one  
> > db or another but I’m not too certain about the usage above…
> 
> The problem isn’t with the params[:keywords] being case sensitive but with  
> the database itself (Postgres). The previous poster sorted this out by  
> mentioning ILIKE. Nothing wring with choosing a database and actually USING  
> it’s specific functions I supoose…

* * *

## not at all - I was just offering a db independent method as opposed to a hard coded db specific method…I use postgres myself but that wasn’t the point.

> > #{@params[:keywords]} which just looks wrong and I would think that you
> 
> Explain? it works perfectly well. “you can execute ruby code in a string  
> by using #{}” which is much neater than “doing” + this + “when” + you +  
> "have alot of " + stuff\_going\_on

* * *

well the + stuff seems more obvious to me but the thing that caught my  
eye was using the instance variable @params which is confusing because  
of the nature of params being the object passed by GET/POST. One of the  
more confusing things to me was the usage of @model vs. @models and what  
I finally figured out was that the difference was more in my own  
expectations and thus the choice of the names of instance variables  
becomes one of easy comprehension.

Craig

---

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [March 13, 2006, 8:27am UTC](https://www.ruby-forum.com/t/case-insensitive-search/79435/6 "2006-03-13T08:27:45Z")

</div>

Rick T. wrote:

> If you’re using MySQL, that should be working fine. Under postgres,  
> you’d need to use “ilike” rather than “like” for a case insensitive

Thanks, I didn’t manage to stumble acrross ILIKE in the Postgres docs  
☹
