Fetching all records corresponding to a set of ids, ignoring invalid ids

by Melvin Ram

Conversation from #rubyonrails IRC channel

pipegeek: Is there a straightforward way, given a list of ids, fetch all corresponding records, ignoring invalid ids?

pipegeek: ie, something like Model.find([1,2,3,4,5]), but without the requirement that all the ids be matched

pipegeek: err, should clarify (if it isn’t already obvious) I’m speaking about activerecord

therrg: pipegeek: Model.find_all_by_id([1,2,3,4,5]) does what you want

pipegeek: oh!  Damn, I feel foolish for not having checked that—figured the find_all_by_ methods behaved the same way

pipegeek: thanks, therrg

therrg: yw

melvinram: btw, what is your usage scenario?

melvinram: trying to see when that would be useful

pipegeek: Well, I have a list of ids generated much earlier by some other tool that added a bunch of records

pipegeek: I need to do something to all of those records.  Only, it’s possible that some have been DELETEd in the meantime

melvinram: ahe i see

Leave a Comment