Skip to content

Instantly share code, notes, and snippets.

@convexstyle
Last active December 22, 2015 08:38
Show Gist options
  • Save convexstyle/6445983 to your computer and use it in GitHub Desktop.
Save convexstyle/6445983 to your computer and use it in GitHub Desktop.
Avoid ActiveRecord::RecordNotFound
#author = Author.find(id) throws ActiveRecord::RecordNotFound error.
#for one record
author = Author.find_by_id(id)
if !author.nil?
#do something
else
#do something
end
#for multiple record (find_all_by_id is deprecated in Rails 4)
authors = Author.where(:id => [id])
if !authors.empty?
#do something
else
#do something
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment