Reputation: 1303
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?
Upvotes: 14
Views: 7274
Reputation: 10150
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.
Upvotes: 4