From the course: Ruby on Rails 6 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Find records using ActiveRecord

Find records using ActiveRecord - Ruby on Rails Tutorial

From the course: Ruby on Rails 6 Essential Training

Start my 1-month free trial

Find records using ActiveRecord

- [Instructor] In any Rails application, you will query for existing records far more often than you will create, update, or delete them. Active record and active relation offer many tools to make finding the right records easier. Let's begin with the simplest method, the primary key finder. This is the type of find that we've been using. We start with the class name, in this case Subject, and then you call the find method on it. And the argument to find is going to be the primary key. Most often that's going to be the ID. So we're asking Rails to go to the Subjects table and find a record who's ID is two. The result will either be an object if it finds the record, or it will raise an error if the record does not exist. You would use the primary key finder when it would be surprising if the object wasn't in the database. For example, if you do a database query and retrieve a list of all customers…

Contents