[Ruby on Rails] find with ActiveRecord::RecordNotFound

Tadashi Shigeoka ·  Mon, December 3, 2012

In Rails, when you pass only an id as an argument to the find method and there are 0 result records, an ActiveRecord::RecordNotFound exception will be thrown.

You can handle the exception:

rescue ActiveRecord::RecordNotFound

Alternatively, you can use find_by_id, which returns nil when there are 0 results.

【Reference】

findよりfind_by_idでActiveRecord::RecordNotFoundを回避する - パンプキンスパイスラテ

That’s all from the Gemba.