# How to do a case insensitive search with multiple elements?

**URL:** <https://www.ruby-forum.com/t/how-to-do-a-case-insensitive-search-with-multiple-elements/191575>\
**Category:** Rails\
**Created:** [July 14, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/how-to-do-a-case-insensitive-search-with-multiple-elements/191575 "2010-07-14T18:30:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hostep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@hostep](https://www.ruby-forum.com/u/hostep)\
**Post date:** [July 14, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/how-to-do-a-case-insensitive-search-with-multiple-elements/191575/1 "2010-07-14T18:30:05Z")

</div>

Hi

I have a smallish problem

If a = [“Joe”,“John”]

then

Model.find\_all\_by\_firstname(a)

works well.

Except when the case of the characters is in dispute - how can I make  
the finder method case insensitive? All the discussions I find concern  
themselves with the singular case, but not with the case of an array  
being passed.

Thanks for reading this!

Pieter

---

<div class="post-metadata">

**Author:** ![hostep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@hostep](https://www.ruby-forum.com/u/hostep)\
**Post date:** [July 15, 2010, 10:05am UTC](https://www.ruby-forum.com/t/how-to-do-a-case-insensitive-search-with-multiple-elements/191575/2 "2010-07-15T10:05:57Z")

</div>

Model.find\_all\_by\_firstname(a.collect{|a| a.downcase})

---

<div class="post-metadata">

**Author:** ![hostep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@hostep](https://www.ruby-forum.com/u/hostep)\
**Post date:** [July 15, 2010, 12:08pm UTC](https://www.ruby-forum.com/t/how-to-do-a-case-insensitive-search-with-multiple-elements/191575/3 "2010-07-15T12:08:41Z")

</div>

On 15 July 2010 09:05, Rob N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Model.find\_all\_by\_firstname(a.collect{|a| a.downcase})

Will that work if the names in the db include upper case chars? I  
think the compare in the find needs to be case insensitive rather than  
what is being compared against being forced to lower case.

Colin

---

<div class="post-metadata">

**Author:** ![hostep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@hostep](https://www.ruby-forum.com/u/hostep)\
**Post date:** [July 15, 2010, 1:18pm UTC](https://www.ruby-forum.com/t/how-to-do-a-case-insensitive-search-with-multiple-elements/191575/4 "2010-07-15T13:18:49Z")

</div>

Colin L. wrote:

> On 15 July 2010 09:05, Rob N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Model.find\_all\_by\_firstname(a.collect{|a| a.downcase})
> 
> Will that work if the names in the db include upper case chars? I  
> think the compare in the find needs to be case insensitive rather than  
> what is being compared against being forced to lower case.
> 
> Colin

If that’s the case, this will do it:

class Model

```
self.find_all_by_firstname(first_name)
  find(:all, :conditions => ["LOWER(firstname) = ?", 

```

first\_name.downcase])  
end

end

---

<div class="post-metadata">

**Author:** ![hostep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@hostep](https://www.ruby-forum.com/u/hostep)\
**Post date:** [July 15, 2010, 1:45pm UTC](https://www.ruby-forum.com/t/how-to-do-a-case-insensitive-search-with-multiple-elements/191575/5 "2010-07-15T13:45:16Z")

</div>

On Jul 15, 2010, at 7:18 AM, Rob N. wrote:

> –

As long as you’re aware that using LOWER(first\_name) will prevent any  
use of an index on first\_name.

You can also look at your particular database to see if it supports a  
case-insensitive match. MySQL is case-insensitive by default (so I’m  
guessing that’s not what you’re using 😉

If you’re using PostgreSQL, you probably could find this via Google:

[http://wiki.postgresql.org/wiki/FAQ#How\_do\_I\_perform\_regular\_expression\_searches\_and\_case-insensitive\_regular\_expression\_searches.3F\_How\_do\_I\_use\_an\_index\_for\_case-insensitive\_searches.3F](http://wiki.postgresql.org/wiki/FAQ#How_do_I_perform_regular_expression_searches_and_case-insensitive_regular_expression_searches.3F_How_do_I_use_an_index_for_case-insensitive_searches.3F)

Note the part that discusses the CITEXT type and the use of an index  
on the lower(column\_name).

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)
