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.

Delete records using ActiveRecord

Delete records using ActiveRecord - Ruby on Rails Tutorial

From the course: Ruby on Rails 6 Essential Training

Start my 1-month free trial

Delete records using ActiveRecord

[Instructor]- We've seen how to create and update records. Now let's learn how to delete them. Deleting records is a two-step process, find and destroy. First you find the record in the database table. Rails uses the data that it gets back to populate, and return an instance of the class. And then you tell that object that it should destroy itself. There's only one pitfall that you need to be careful of. We're talking about deleting records. But the method we're going to be using is called destroy. Destroy, Will create an SQL delete statement. There's also another method in rails called delete. It also sends an SQL delete statement. These two methods may seem to work the same way, but delete bypasses some rails features, and might give you unexpected behavior. Most of the time, the best method to use is destroy. If you're still in the rails console from the last preview, that's fine, there's no need to exit, but I'm going to…

Contents