Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources A Route to a Delete Action Rails Resources

Brendan Whiting
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,698 Points

Why is 'delete' renamed to 'destroy'?

I understand that this is the convention in Rails and it automatically generates these routes for me as described in the video. Just wondering if there was a reason why. Maybe it's just the HTTP verb world and the Active Record world evolving independently of each other (But SQL calls it DELETE... hmm)

1 Answer

Hi Brendan,

I think delete is the simple implementation - it deletes a row of the database but doesn't tidy up related dependencies. Using the destroy action in the controller allows you to manage dependencies, which keeps the data base clean using callbacks like before_destroy.

Also, delete just bins the record. I think destroy holds the instance in memory so you can use its properties one more time, like "User #{user.id} has been deleted".

I did a Google for a better explanation and found this:

Basically "delete" sends a query directly to the database to delete the record. In that case Rails doesn't know what attributes are in the record it is deleteing nor if there are any callbacks (such as before_destroy).

The "destroy" method takes the passed id, fetches the model from the database using the "find" method, then calls destroy on that. This means the callbacks are triggered.

You would want to use "delete" if you don't want the callbacks to be triggered or you want better performance. Otherwise (and most of the time) you will want to use "destroy".

I hope that helps,