kabir kCourses Plus Student 18,036 Points
destroy vs. delete
In the Deleting Todo Lists video (00:50), Jason uses
Destroy in the table row for
Why not Delete? And what's the difference between the two?
Tim Knight28,862 Points
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.
The destroy and destroy_all methods are used to remove specified associations from the database, or all of them, respectively. Both methods operate transactionally.
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.