Tags

,

At first look, the code below seems ok and you’ll got the expected results.

articles = [786, 2470, 2470, 2471, 2471, 2475, 2475]
@article_list = Articles.find( articles.map(& :id), : order => "title asc, created_at desc")

However, if one of the ID don’t exist in the ‘Article’ table then it raises ActiveRecord::RecordNotFound and the script stops.
Ex. ActiveRecord::RecordNotFound: Couldn’t find Article with IDs (786, 2470, 2470, 2471, 2471, 2475, 2475) (found 1 results, but was looking for 2)

To solve the problem, you need to change the code into this:

@article_list = Articles.where(:id=> articles.map(&:id)).order("title asc, created_at desc")

and it works!

REF: http://pjkh.com/articles/rails-couldnt-find-all-xxx-with-ids-found-1-but-was-looking-for-2/

Advertisements