Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 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,

Steve.