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,862 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.