Here is a simple tip to handle Record not found exception.
I found many programmers uses code like if User.find(33) then do something. If the id is not present then Active Record throws Record not found Exception which can be caught and handled using begin-rescue. But the simple way is?
ActiveRecord::RecordNotFound: Couldn’t find User with ID=45
A better approach would be to check whether record exists in database find_by_id helper and/or exists?
User.find_by_id(id) returns the record if found or else returns nil.
User.exists?(id) returns true if the record is found else returns false.
Hope you will like this simple tip, if you know some other right and short way, please post it as a comment.
Record not found – A simple way to handle it.
39 sec read