DEV Community

Cover image for To find or to find_by, that is the question
Desiree Lerma
Desiree Lerma

Posted on • Updated on

To find or to find_by, that is the question

This week I created my second project with the Flatiron school using Ruby's ActiveRecord and Sinatra's framework. In doing so, I found myself using ActiveRecord's (one of many) helpful finder methods . While these methods helped me immensely when I was searching for a specific instance of a class. However, I also realized I was struggling a bit with a simple issue. That issue being what are my required arguments when using one or the other? Silly, I know, but are you actually a developer if you don't occasionally need a second pair of eyes to catch tiny issues such as this one?

team-looking-at-comp

.find(id)

Let's start with the bare bones, basic find method. Using this method requires an integer to be passed through as an argument. This is because the find method takes that integer and finds its matching partner. Its partner being the primary key id of the object that find is being called upon.

.find code snippet

Using find is a great tool if you are looking for a specific id. You may even search for multiple id's at once! Of course, if ActiveRecord cannot find a match you receive a lovely error saying "ActiveRecord::RecordNotFound," meaning that id doesn't exist in your data structure. Very straight forward to avoid any confusion.

.find_by(key: "value")
.find_by_key("value")

On the other hand, if you're looking to make your search a bit more specific (because let's face it, you won't always be able to remember every single id that gets assigned upon creation of an instance) you can utilize the glamorous find_by method. This method is an exceptionally useful one because you're able to perform a more focused search on your object. You can perform this search by simply adding the key value pair you are attempting to locate, within the data structure, to your method.

Do take note that find_by will only return the first match it encounters. If you have multiple keys with the same value associated to it, you will only receive back the first one that ActiveRecord locates. If no match is found, the error you receive is different than the one received when using the regular ole find method. If find_by cannot be matched a simple "nil" is returned.

A fun thing about find_by that I would like to include is that you can tack additional queries onto the method (before the argument)! The more you know.

.find_by snippet

Here's a little bonus for you!

User.where(key: "value")

Another valuable query method ActiveRecord provides for its users is the where method. The where method works almost identically as the find_by method does with only one exception. The where method returns an ActiveRecord::Relation which is essentially a collection of ActiveRecord objects. The arguments passed through a where method can come in the form of a string, an array, or a hash. If ActiveRecord cannot find a match to your query using the where method, the return is an empty ActiveRecord::Relation (ActiveRecord::Relation[]).

.where snippet

Now, hopefully I've provided you with a greater understanding of the different query methods ActiveRecord provides. With this information, whether it be new, more in depth or a simple refresher I hope you'll feel more confident in utilizing these methods. Query away my fellow developers!

magnifying glass

Resources:

Top comments (1)

Collapse
 
linuxander profile image
Linuxander • Edited

Nice post! I just started with some benchmark measures for find, find_by and where, and saw this post. Have you already done this, maybe?