Share This Article
The ActiveRecord find_by
method is a shorthand method for finding one ActiveRecord object using one of the model’s attributes.
What is ActiveRecord find_by Used For?
The find_by
method queries your database for a specific record or records that match certain criteria.
How to Use ActiveRecord find_by
The basic syntax for using find by is this:
Model.find_by(attribute: value)
For example, if you have a users table and you want to find the user with an id
of 1, you can do it like this:
user = User.find_by(id: 1)
Similarly, you can find a user by their name:
user = User.find_by(name: "John")
Or you can find a user by multiple attributes:
user = User.find_by(name: "John", age: 30)
It’s also possible to use the find_by
method to find all users that match certain criteria:
users = User.find_by(name: "John", age: 30)
users.each do |user|
puts user.name
end
If you want to find multiple records and you know there will be more than one record that meets your criteria, you should use the where
method instead of find_by
.
The Difference Between ActiveRecord find_by and where
The main difference between the find
method and the find_by
method is that find
expects an id as an argument, while find_by
expects an attributes hash:
User.find(1)
# Find by id
User.find_by(name: "John")
# Find by attributes
Another thing to keep in mind is that find_by
will only return the first record it finds, while the where
method will return an array with all the records it finds:
User.where(name: "John")
# Returns an array of users
The find_by
method is a convenience method that Rails provides for us.
If you want to find a specific record using an id, you should use the find
method:
User.find(1)
If you want to do a more complex search, where you can search by multiple attributes, you should use the where
method:
User.where(name: "John", age: 30)
For more complex searches, the where
method is going to be more flexible because you can do things like finding all users that have an id greater than a certain number:
User.where("id > ?", 10)
find_or_create_by
The find_or_create_by
method is similar to the find_by
method, but it will create the object if it doesn’t exist.
For example, let’s say we have a User model and want to find the first user named “John Doe,” but if there is no user with that name, we want to create one.
We could do that like this:
user = User.find_or_create_by(name: "John Doe")
puts user.name
# John Doe
puts user.age
# nil
first_or_create
The first_or_create
method is similar to find_or_create_by
, but it will use the order
method to find the first object.
For example, let’s say we have a User model and we want to find the first user named “John Doe”, but if there is no user with that name, we want to create one.
We could do that like this:
user = User.where(name: "John Doe").first_or_create
puts user.name
# John Doe
puts user.age
# nil
first_or_initialize
The first_or_initialize
method is similar to first_or_create
, but it will not save the object to the database.
For example, let’s say we have a User model and want to find the first user named “John Doe”, but if there is no user with that name, we want to create one.
We could do that like this:
user = User.where(name: "John Doe").first_or_initialize
puts user.new_record?
# true
user.save
puts user.new_record?
# false
find_by! vs find_by
The find_by
method returns nil
if the object is not found, while the find_by!
method raises an ActiveRecord::RecordNotFound
exception.
Finally, it is essential to note that the find_by
method is not a Rails method but a Ruby method defined on the Enumerable module.
You can use this method with any Enumerable object, not just an ActiveRecord.
Summary
In this article, you’ve learned how to use the find_by
and where
methods as well as the find_or_initialize
, first_or_create
and first_or_initialize
methods.
You’ve learned the main differences between these methods & how to use them to find a specific record or records that match certain criteria.
You’ve also learned that find_by
is not a Rails method, it’s just a Ruby method, and you can use it with any Enumerable object.