I know from the rails documentation that find
will result in a RecordNotFound
error. However, the find_by
method simply returns nil
if a record is not found. Returning nil
seems more intuitive to me and safe in general, so I am wondering if there is any benefit in using the find
method over the find_by
method?
For example, what is the difference between the following:
myRecord.find(1)
and
myRecord.find_by(id: 1)
If the only difference is that find
raises an error when a record isn't found, I don't really see the benefit in using find
.
EDIT
For all the people that jumped on my question and said that it had been already answered, you are wrong. I clearly stated that I knew find
returns an error when a record is not found (which is what everyone else emphasizes in their answers) and that find_by
returns nil. I want to know if there are any other differences.