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 Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Deleting Todo Lists

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

destroy vs. delete

In the Deleting Todo Lists video (00:50), Jason uses Destroy in the table row for Show Edit Destroy.

Why not Delete? And what's the difference between the two?

1 Answer

Tim Knight
Tim Knight
28,888 Points

kabir,

Destroy is just a common word used in the Rails community for delete buttons (when generated through scaffolding like they were in this case) because of the matching method of the same name. There are differences too in terms of the two methods in a Rails application too. We call destroy on a method over delete because it gives us the opportunity to use various ActiveRecord callbacks. From The Rails 4 Way book:

The delete and delete_all methods are used to sever specified associations, or all of them, respectively. Both methods operate transactionally.

It continues...

The destroy and destroy_all methods are used to remove specified associations from the database, or all of them, respectively. Both methods operate transactionally.

And finally...

When triggered, :destroy will call the dependent’s callbacks, whereas :delete will not.

So in the case of setting your dependencies you can set dependent: :destroy or dependent: :delete and that dependency call back will be executed.