Hi Friends,
Rails provides several methods for fetching single object from table, one of the method is find_by. It fetches the first record in the table matching some specific conditions. Example can be if we want to fetch the first blog with title "Active Records in Rails", then we will use:
Blog.find_by title: 'Active Records in Rails'
# => #<blog id:1,title:"active records in rails"></blog>
It will return nil, if no record matches the specified criteria.
One more similar method is find_by! the difference is that it returns ActiveRecord::RecordNotFound if no record is found with specified criteria. Example:
Blog.find_by! title: 'Not found'
# => ActiveRecord::RecordNotFound
0 Comment(s)