Deploy apps to servers that you own and control.
Application monitoring that helps developers get it done.
Now that we know how to create and update data in our tables, let’s do a quick introduction to querying data using Active Record.
Jumping back into our playground, we have our posts table with title and body columns, our Post model, and two records already inserted. Now, let’s explore some core query methods that are essential for working with Active Record.
Of course, there are many more methods, and you can find a link to the Active Record guide in the documentation below, which breaks down all public methods available for reading from the database. For now, we’ll focus on a handful of key queries.
The most important query method to know is where, which allows us to build conditional SELECT queries.
For example, if we search for posts where the title is "Title 1":
Post.where(title: "title one")
Active Record generates the following SQL query:
SELECT * FROM posts WHERE title = ?
Here’s what’s happening:
If we pass an array of values, Active Record uses the IN operator instead of =:
Post.where(title: ["title one", "other title"])
This generates:
SELECT * FROM posts WHERE title IN (?, ?)
This acts like an OR condition, returning all records that match either of the values.
If we want to retrieve a single record instead of an array of results, we have two options:
ruby
Post.find(1)
This directly queries by primary key (id) and returns one record. If no record is found, it raises an error.
ruby
Post.find_by(title: "title one")
This is useful when you don’t know the id but have a unique field. Unlike find, this does not raise an error if no record is found—it simply returns nil.
By default, queries return results in the order they were inserted. If we want to control sorting, we can use order:
Post.where(title: ["title one", "title two"]).order(:title)
This generates:
SELECT * FROM posts WHERE title IN (?, ?) ORDER BY title ASC
To control the sort direction, we can specify ascending (ASC) or descending (DESC):
Post.order(title: :desc)
Post.order(title: :desc, body: :asc)
This ensures that ties in title are resolved using body.
Active Record is powerful, but sometimes you need more complex conditions. You can pass raw SQL inside where:
Post.where("created_at > ?", Time.now)
This generates:
SELECT * FROM posts WHERE created_at > ?
Active Record safely binds the value to prevent SQL injection.
If we change the condition to find older posts:
Post.where("created_at < ?", Time.now)
This returns all records created before the current time.
Method | Returns | Best Use Case |
---|---|---|
where | Array of records | Filtering by one or more conditions |
find | Single record (or error) | Fetching a record by ID |
find_by | Single record (or nil) | Finding by a unique attribute |
order | Array of records | Sorting results |
There are many additional methods in Active Record for complex queries, grouping, and conditions. If you’re interested in learning more, check out the Active Record guide linked below.
In the next video, we’ll explore more advanced querying techniques, including aggregations and joins.