The Active Record pattern is an ORM (Object-relational mapping) technique for converting data between incompatible type-systems in object-oriented programming languages. In today’s post, we’ll be covering how to perform some different database queries using Rails’ Active Record Query Interface.
Active Record provides several finder methods to retrieve objects from the database. Each of these methods allow us to pass arguments into them to perform database queries without our having to write SQL.
Retrieve a Single Database Object
To find a single object in our database, we use the
.find method, passing in the desired object’s primary_key as an argument.
Here’s a quick visual aid:
There are also a few Rails methods for finding the first and last records in our database.
not surprisingly, these methods are named
Even if we wanted to ensure that those records actually exist while calling them, Rails allows us to add a bang to the end of these methods like so:
The significance of the bang symbol is that Rails will raise a RecordNotFound exception if there are no matching objects found.
Retrieve Multiple Objects in Batches
I like the first example in the docs on this, because as a wet behind the ears developer, I have primarily stuck to using the Enumerator method
.each to cycle through a list of results. But as the docs state, inevitably as my table grows in size, the amount of memory that it takes to house the array full of data may not be enough to perform the task with satisfactory performance results, in other words our application could slow down enough to annoy our users.
Again, Rails provides two methods addressing this problem case, by splitting records into memory-friendly batches for processing.
.find_each method retrieves a batch of a thousand records at a time, and then yields each record to the block individually. There are a few options for the
.find_each method, the first is called
:batch_size and allows us to specify the number of records to be retrieved in each batch.
Take a peak:
There is also a :start option that can be exercised in the
.find_each method. This option allows us to configure the first ID of the sequence whenever the lowest ID is not the one you need.
where method allows us to specify conditions to limit the records returned, representing the
WHERE of an SQL statement.
Here are some examples using Rails’ pure string conditions:
We can use array conditions for those cases when our search query could vary like so:
The second expression in the above example demonstrates those cases when we want to specify more than one condition in our query.
Ordering our Queries
To aid us in the retrieval of multiple records from our database in a specified order, we can use Active Record’s
The first line of code in our example simply orders Employee by the date of object creation. The second and third expressions demonstrates how to order in an ascending and descending fashion. And the fourth expression in the above example illustrates how we could order our query results by more than one field at a time.
This concludes our brief walk-thru of Active Record 101. If there are some methods that you feel should be added to a new developer’s understanding of querying a database, feel free to leave a comment.