find vs find_by vs where
where returns ActiveRecord::Relation
Now take a look at find_by implementation:
def find_by
where(*args).take
end
As you can see find_by is the same as where but it returns only one record. This method should be used for getting 1 record and where should be used for getting all records with some conditions.
Edit: This answer is very old and other, better answers have come up since this post was made. I'd advise looking at the one posted below by @Hossam Khamis for more details.
Use whichever one you feel suits your needs best.
The find
method is usually used to retrieve a row by ID:
Model.find(1)
It's worth noting that find
will throw an exception if the item is not found by the attribute that you supply. Use where
(as described below, which will return an empty array if the attribute is not found) to avoid an exception being thrown.
Other uses of find
are usually replaced with things like this:
Model.all
Model.first
find_by
is used as a helper when you're searching for information within a column, and it maps to such with naming conventions. For instance, if you have a column named name
in your database, you'd use the following syntax:
Model.find_by(name: "Bob")
.where
is more of a catch all that lets you use a bit more complex logic for when the conventional helpers won't do, and it returns an array of items that match your conditions (or an empty array otherwise).
Model.find
1- Parameter: ID of the object to find.
2- If found: It returns the object (One object only).
3- If not found: raises an ActiveRecord::RecordNotFound
exception.
Model.find_by
1- Parameter: key/value
Example:
User.find_by name: 'John', email: '[email protected]'
2- If found: It returns the object.
3- If not found: returns nil
.
Note: If you want it to raise ActiveRecord::RecordNotFound
use find_by!
Model.where
1- Parameter: same as find_by
2- If found: It returns ActiveRecord::Relation
containing one or more records matching the parameters.
3- If not found: It return an Empty ActiveRecord::Relation
.
There is a difference between find
and find_by
in that find
will return an error if not found, whereas find_by
will return null.
Sometimes it is easier to read if you have a method like find_by email: "haha"
, as opposed to .where(email: some_params).first
.