From the course: Ruby on Rails 5 Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Delete records - Ruby on Rails Tutorial
From the course: Ruby on Rails 5 Essential Training
Delete records
- [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 and we call it find and destroy. There's finding the record, just like we when we were updating the record, and then we destroy the record. Now here's a gotcha that you need to be careful of. There's also a method in Rails called delete and it may seem to work the same as destroy does. But delete bypasses some Rails features and might give you unexpected behavior. Destroy is the best method to stick with as a beginner. If you're still in the Rails console, that's fine, there's no need to exit and go back in, but I'm going to start from scratch. And I'm going to first create a new subject. Let's do that using Subject.create and I'm going to give it a name and I'm going to call it, Bad subject. And that's going to save it to the database. I give it an ID of three so I can find that subject as my first step as subject, find, three. Just clear my screen…
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.