{"id":157,"date":"2014-03-13T16:26:39","date_gmt":"2014-03-13T23:26:39","guid":{"rendered":"http:\/\/www.tizianasellitto.it\/blog\/?p=157"},"modified":"2020-05-11T09:47:59","modified_gmt":"2020-05-11T16:47:59","slug":"destroy-vs-delete-in-rails","status":"publish","type":"post","link":"https:\/\/www.tizianasellitto.it\/blog\/?p=157","title":{"rendered":"Destroy vs Delete in Rails"},"content":{"rendered":"<p style=\"text-align: justify;\">It has been not really clear in my mind the difference between &#8220;destroy&#8221; and &#8220;delete&#8221; for a while before attempting to delete an object and its related once. In this situation and, reading carefully the documentation and the source code, I have discovered that was impossible since <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveRecord\/Relation.html#method-i-delete\" target=\"_blank\" rel=\"noopener noreferrer\">delete<\/a> is used to delete the objects without instantiating it and executing the callbacks, including :dependent for associations. While using <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveRecord\/Relation.html#method-i-destroy\" target=\"_blank\" rel=\"noopener noreferrer\">destroy<\/a> the object is instantiated first and so callbacks and filters are triggered.<\/p>\n<p style=\"text-align: justify;\">Assuming the existence of two model a Customer and an Address. With a relation of has_many that connects them:<\/p>\n<p style=\"text-align: left;\"><code>class Customer &lt; ActiveRecord::Base<br \/>\nhas_many :addresses, dependent: :destroy<br \/>\nend<br \/>\nclass Address &lt; ActiveRecord::Base<br \/>\nbelongs_to :customer<br \/>\nend<br \/>\n<\/code><\/p>\n<p style=\"text-align: left;\">I&#8217;ve tried to use delete to remove the customer from the database but all the addresses were pending in the database orphans. Then I&#8217;ve used &#8220;destroy&#8221; to destroy the customer and all the addresses related.<\/p>\n<p style=\"text-align: left;\"><code>Customer.destroy(params[:id])<br \/>\n<\/code><\/p>\n<p style=\"text-align: left;\">In this way the customer and all the addresses (marked using &#8220;dependent&#8221; option) are deleted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It has been not really clear in my mind the difference between &#8220;destroy&#8221; and &#8220;delete&#8221; for a while before attempting to delete an object and its related once. In this situation and, reading carefully the documentation and the source code, I have discovered that was impossible since delete is used to delete the objects without [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[32,20],"_links":{"self":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/157"}],"collection":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":6,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}