# Find( \[1,2,3\] ) find from array without raising error

**URL:** <https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324>\
**Category:** Rails\
**Created:** [August 29, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324 "2007-08-29T20:55:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/1 "2007-08-29T20:55:26Z")

</div>

Hello!

Best way to explain this is with some code…

id\_arr = [1,2,3,4,5] # There is no record with id 5.  
=\>  
results = find(id\_arr)  
ActiveRecord::RecordNotFound: Couldn’t find all Posts with IDs  
(1,2,3,4,5).

From reading the documentation it appears that AR will produce this  
error if ALL the ids do not return errors, but it appears to error when  
one or more of the ids does not return.

Any way to get those id’s which do exist to return, without the error?

Cheers.

Douglas F Shearer  
removed\_email\_address@domain.invalid

> **[Douglas F Shearer](https://douglasfshearer.com)**
>
> Software developer, mountainbiker.

---

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/2 "2007-08-29T21:05:44Z")

</div>

Douglas S. wrote:

> From reading the documentation it appears that AR will produce this  
> error if ALL the ids do not return errors, but it appears to error when  
> one or more of the ids does not return.
> 
> Any way to get those id’s which do exist to return, without the error?

From memory (I don’t remember if you need something like  
id\_arr.join(’,’) in the conditions instead of just id\_arr):

results = id\_arr.empty? ? [] : find(:all, :conditions =\> [ ‘id IN (?)’,  
id\_arr ])

Lionel

---

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/3 "2007-08-29T21:10:39Z")

</div>

Lionel B. wrote:

> results = id\_arr.empty? ? [] : find(:all, :conditions =\> [ ‘id IN (?)’,  
> id\_arr ])

find(:all) seems to be the way to go, came up with the following just  
after I posted…

find(:all, :conditions =\> 'id = ’ + id\_arr.inject{ |sum,i| sum.to\_s + ’  
OR id = ’ + i.to\_s })

… but yours is far cleaner, I think I shall go with that.

Cheers.

Douglas F Shearer  
removed\_email\_address@domain.invalid

> **[Douglas F Shearer](https://douglasfshearer.com/)**
>
> Software developer, mountainbiker.

---

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/4 "2007-08-29T21:17:47Z")

</div>

Nicer to do:

Thing.find(:all, :conditions =\> [‘id IN (?)’, id\_arr])

On Aug 29, 8:10 pm, Douglas S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/5 "2007-08-29T21:10:46Z")

</div>

On 8/29/07, Douglas S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From reading the documentation it appears that AR will produce this  
> error if ALL the ids do not return errors, but it appears to error when  
> one or more of the ids does not return.
> 
> Any way to get those id’s which do exist to return, without the error?

There may be an easier way, but something like this will do it:

results = id\_arr.collect {|i| Model.find\_by\_id(i)}.compact

or, more efficient, since it makes only one query:

results = Model.find :all, :conditions =\> [“id in (#{id\_arr.collect  
{‘?’}.join(‘,’)})”, \*id\_arr]

---

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/6 "2007-08-29T21:26:49Z")

</div>

On 8/29/07, andrewbruce [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Nicer to do:
> 
> Thing.find(:all, :conditions =\> [‘id IN (?)’, id\_arr])

Hmm, I didn’t know that an array was automagically expanded in a  
parameter list like that. Handy, and much cleaner than my line noise  
:~)

---

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/7 "2007-08-29T21:22:47Z")

</div>

andrewbruce wrote:

> Thing.find(:all, :conditions =\> [‘id IN (?)’, id\_arr])

Great minds think alike! Nice to get confirmation that my (Slightly late  
relative to my original posting) idea is correct.

Thanks guys! :o)

Douglas F Shearer  
removed\_email\_address@domain.invalid

> **[Douglas F Shearer](https://douglasfshearer.com/)**
>
> Software developer, mountainbiker.

---

<div class="post-metadata">

**Author:** ![dougal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@dougal](https://www.ruby-forum.com/u/dougal)\
**Post date:** [August 29, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/find-1-2-3-find-from-array-without-raising-error/113324/8 "2007-08-29T22:00:57Z")

</div>

Bob S. wrote:

> 

But depending on the context you can have to test for id\_arr.empty? …
