# Searching for multiple possible values

**URL:** <https://www.ruby-forum.com/t/searching-for-multiple-possible-values/149178>\
**Category:** Rails\
**Created:** [September 22, 2008, 9:17am UTC](https://www.ruby-forum.com/t/searching-for-multiple-possible-values/149178 "2008-09-22T09:17:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [September 22, 2008, 9:17am UTC](https://www.ruby-forum.com/t/searching-for-multiple-possible-values/149178/1 "2008-09-22T09:17:50Z")

</div>

Hi,  
I’m trying to do an search for multiple possible values in one  
database field. I have an array and want to find any of the values in  
the array in the database and return the results.

So basically  
arr = [‘item1’, ‘item2’, ‘item3’]

and I want to Model.find :all, :conditions =\> {:items =\> arr}  
and find all the entries where :items equals any of the strings in  
array.

What is the preferred way to do this?

Thanks,  
JB

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [September 22, 2008, 9:27am UTC](https://www.ruby-forum.com/t/searching-for-multiple-possible-values/149178/2 "2008-09-22T09:27:15Z")

</div>

On Sep 22, 8:17 am, Jables [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> array.  
> Assuming :items is the name of a database field, that will do exactly  
> what you want (ie it will generate "items in (‘item1’, ‘item2’,  
> ‘item3’))

Fred

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [September 22, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/searching-for-multiple-possible-values/149178/3 "2008-09-22T20:45:08Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [September 22, 2008, 9:37am UTC](https://www.ruby-forum.com/t/searching-for-multiple-possible-values/149178/4 "2008-09-22T09:37:47Z")

</div>

On Mon, Sep 22, 2008 at 9:17 AM, Jables [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and find all the entries where :items equals any of the strings in  
> array.

IN is supposed to be faster than OR. In addition {:items =\> arr} is  
very concise, that’s the recommended solution.

Take into account that if the array is huge the SQL may be too large.  
In MySQL for example IN() has no limit in the number of arguments it  
may receive, but a single SQL over the wire cannot exceed  
max\_allowed\_packet.
