find vs. find_by in Ruby on Rails

In Ruby on Rails, when working with Active Record, we often encounter situations where we need to retrieve records from the database based on certain conditions. Two common methods for this are find and find_by. While they might seem similar, they have distinct differences in how they retrieve records and handle conditions.

The find method

The find method in Rails is primarily used to retrieve a single record by its primary key (id). It is commonly used when we know the exact ID of the record we want to fetch. The syntax for find is straightforward:

Model.find(id)
Syntax for the find method

Example

Let’s say we have a Task model, and we want to retrieve the task with ID 1:

Task.find(1)
find method to fetch the task with id 1

Note: You can try executing this command in the sample application provided at the end.

Behavior

  • If the record with the specified ID is found, find returns that record as an Active Record object.

  • If the record is not found, it raises an ActiveRecord::RecordNotFound exception. This means we need to handle the exception, typically with a rescue block. For example:

begin
task = Task.find(1)
rescue ActiveRecord::RecordNotFound
puts "Task with id 1 not found."
end
Handle the RecordNotFound exception

The find_by method

The find_by method is used to retrieve the first record that matches the specified conditions. Unlike find, which only works with primary keys, find_by allows us to query based on any attribute or combination of attributes. The syntax for find_by is as follows:

Model.find_by(attribute: value)
Syntax for find_by method

Example

Suppose we want to find a task with a specific title:

# Find the task based on spacific ID
Task.find_by(id: 1)
# Find the task based on specific title
Task.find_by(title: "Task 1")
find_by method to fetch the task with specific attribute

Note: You can try executing these commands in the sample application provided at the end.

Behavior

  • find_by returns the first record that matches the specified conditions as an Active Record object.

  • If no record matches the conditions, it returns nil.

Differences and use cases

Let’s look at some differences between find and find_by in Rails and explore some of their use cases as well:

1. Query handling

  • find: This is ideal when we know the exact ID of the record we want to fetch. It is useful for fetching a specific record by its primary key.

  • find_by: This allows querying based on attributes. It is useful for finding the first record that matches specific conditions.

2. Return value

  • find:

    • This returns the record as an Active Record object.

    • This raises an exception (ActiveRecord::RecordNotFound) if the record is not found.

  • find_by:

    • This returns the first record that matches the conditions.

    • This returns nil if no record matches the conditions.

3. Handling multiple records

  • find: This fetches a single record by ID and does not support fetching multiple records at once.

  • find_by: This fetches the first record that matches the conditions. and is useful when we expect multiple records but only need the first one.

4. Performance considerations

  • find: This is optimized for fetching records by primary key, which is typically indexed for quick lookups.

  • find_by: This executes a SQL LIMIT 1 query to retrieve the first matching record. It is efficient when querying indexed columns. However, retrieving large datasets without proper indexing can negatively affect performance

Test it yourself

Click the “Run” button below to execute the following Rails application. It will launch the Ruby console after setting up the application. You can try find or find_by commands in the terminal.

Note: The following application contains three tasks in the Active Record with the ID 1,2 and 3. You can verify the total number of tasks by executing Task.all command in the Ruby console.

--require spec_helper
Try it yourself

Conclusion

In summary, find and find_by are both powerful methods for retrieving records from the database in Ruby on Rails. The key differences lie in their use cases, return values, error handling, and performance considerations.

Understanding these differences allows us to choose the most suitable method for our specific use case, ensuring efficient and effective database queries in our Rails applications.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved