Last active
December 22, 2015 08:38
-
-
Save convexstyle/6445983 to your computer and use it in GitHub Desktop.
Avoid ActiveRecord::RecordNotFound
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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