Is it possible to use a function as get_by_id_or_404 on rails. For example, in my controller I use :
@destination = Destination.find_by_id(params[:id])
if the id isn’t set or the destination not found, how can I ask Rails to redirect to a 404 page?
Thank you!
In production mode, Rails automatically rescues the
ActiveRecord::RecordNotFoundexception by rendering the 404 error page.Simply use the bang version of the finder that raises
ActiveRecord::RecordNotFoundwhen no result is found.However, in your case there’s no reason to use the
find_bymethod. Simply fallback tofind.