find( [1,2,3] ) find from array without raising error.

Douglas Shearer wrote:


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?    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 ])


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 :~)

Bob Showalter wrote: