find() with nil when there are no records

Solution 1:

Yes, just do:

Challenge.find_by_id(10)

For Rails 4 and 5:

Challenge.find_by(id: 10)

Solution 2:

In Rails 4, dynamic finders - such as find_by_id which was used in the accepted answer - were deprecated.

Moving forward, you should use the new syntax:

Challenge.find_by id: 10

Solution 3:

you can do this a bit hackish, just use the ActiveRecord Query Interface.

this will return nil, instead of raising a Exception

  User.where(:id => 10).first