# Extract find conditions from array

**URL:** <https://www.ruby-forum.com/t/extract-find-conditions-from-array/76827>\
**Category:** Rails\
**Created:** [November 21, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/extract-find-conditions-from-array/76827 "2006-11-21T15:14:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 21, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/extract-find-conditions-from-array/76827/1 "2006-11-21T15:14:27Z")

</div>

Hi,

I have an array that contains list IDs. I want to be able to use all of  
those list IDs to create an OR query.

My code is:

Contact\_List.find(:all,  
:select =\> ‘DISTINCT contact\_id’,  
:conditions =\> [‘list\_id = ?’, params[:joblist][:list\_id]])

but this yields the following query which is obviously incorrect:

SELECT DISTINCT contact\_id FROM contact\_lists WHERE (list\_id = ‘2’,‘3’)

How can I get a query that reads:

SELECT DISTINCT contact\_id FROM contact\_lists WHERE (list\_id = ‘2’ OR  
list\_id = ‘3’)

Thanks,

David

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 21, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/extract-find-conditions-from-array/76827/2 "2006-11-21T15:28:07Z")

</div>

David L. wrote:

> Hi,
> 
> I have an array that contains list IDs. I want to be able to use all of  
> those list IDs to create an OR query.
> 
> My code is:
> 
> Contact\_List.find(:all,  
> :select =\> ‘DISTINCT contact\_id’,  
> :conditions =\> [‘list\_id = ?’, params[:joblist][:list\_id]])
> 
> but this yields the following query which is obviously incorrect:
> 
> SELECT DISTINCT contact\_id FROM contact\_lists WHERE (list\_id = ‘2’,‘3’)
> 
> How can I get a query that reads:
> 
> SELECT DISTINCT contact\_id FROM contact\_lists WHERE (list\_id = ‘2’ OR  
> list\_id = ‘3’)
> 
> Thanks,
> 
> David

Using an “in” clause should accomplish the same thing:

Contact\_List.find(:all,  
:select =\> ‘DISTINCT contact\_id’,  
:conditions =\> [‘list\_id in ?’, “(’” +  
params[:joblist][:list\_id].join("’,’") + “’)”])

No chance to test for you, but I think the above should result in:

SELECT DISTINCT contact\_id FROM contact\_lists WHERE (list\_id in  
(‘1’,‘2’,‘3’,‘4’))

And give you the results you’re after.

c.

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 21, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/extract-find-conditions-from-array/76827/3 "2006-11-21T16:21:29Z")

</div>

Cayce B. wrote:

> Using an “in” clause should accomplish the same thing:  
> Contact\_List.find(:all,  
> :select =\> ‘DISTINCT contact\_id’,  
> :conditions =\> [‘list\_id in ?’, “(’” +  
> params[:joblist][:list\_id].join("’,’") + “’)”])

Also no testing, but I believe Rails will do the joining for you (which  
is partly why his original attempt resulted in the way it did). So the  
above code can be simplified to:

Contact\_List.find :all, :select =\> ‘DISTINCT contact\_id’,  
:conditions =\> [‘list\_id IN (?)’, params[:joblist][:list\_id]]

Eric

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 21, 2006, 4:43pm UTC](https://www.ruby-forum.com/t/extract-find-conditions-from-array/76827/4 "2006-11-21T16:43:45Z")

</div>

On 11/21/06, David L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Contact\_List.find(:all,  
> :select =\> ‘DISTINCT contact\_id’,  
> :conditions =\> [‘list\_id = ?’,  
> params[:joblist][:list\_id]])

Try with this:

Contact\_List.find\_by\_list\_id(params[:joblist][:list\_id]], :select =\>  
‘DISTINCT contact\_id’)
