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 - Ruby on Rails Tutorial
From the course: Ruby on Rails 6 Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Understanding ActiveRecord and ActiveRelation4m 32s
-
(Locked)
Using the Rails console to interact with a Rails project3m 36s
-
Create records using ActiveRecord5m 11s
-
(Locked)
Update records using ActiveRecord3m 53s
-
(Locked)
Delete records using ActiveRecord3m 18s
-
(Locked)
Find records using ActiveRecord6m 33s
-
(Locked)
Define one-to-many associations between models6m 30s
-
(Locked)
-
-
-