views:

32

answers:

2

I have this code:

Article.find([1,2,3])

But only record 1 and 2 exist in the database. I get this exception:

"ActiveRecord::RecordNotFound (Couldn't find all Offers with IDs (1,2,3) (found 2 results, but was looking for 3))"

Is there a way to only get the existing records and not the exception?

+5  A: 

Article.find_all_by_id([1,2,3]) is the way to go!

remi
+1  A: 

The problem is that using find with ids raises exceptions when the records are missing. You could try something like

Article.all :conditions => ["id in (?)", [1,2,3]]

or more succinctly

Article.find_all_by_id [1,2,3]

using find :all does not raise errors on missing records, so even if the records are missing you are cool.

BaroqueBobcat