Three Ways to Make Database Queries in Rails
In Rails, querying the record(s) from database can be done through the following methods:
find(*arguments)
- Find records by their IDs
- Raises exception (ActiveRecord::RecordNotFound) if no record is found
find_by(argument, *arguments)
- Finds the first record matching the specified conditions
- Returns
nil
if no record is found find_by!(argument, *arguments)
raises ActiveRecord::RecordNotFound likefind()
if no record is found
- where(options = :chain, *rest)
- Refer to How to Use The Rails Where Method (with Examples), starting from “Basic Where Conditions”, for examples and their use cases