From 39806b6d98c1dbcbaac6748274e2cc3fbfba3495 Mon Sep 17 00:00:00 2001 From: "Marcel M. Cary" Date: Tue, 13 Nov 2018 15:29:43 -0800 Subject: Describe how has_many's :dependent option affects #delete I was puzzled about why `collection=` was destroying the removed records on an association with `dependent: :destroy`, even after consulting the documentation for that option. I had to dive into the Active Record source to understand what was going on: eventually `collection=` calls `collection.delete` on the ousted records, and it also uses the `:dependent` option to decide how to remove records. It would have helped me to have mention of this in the documentation for `:dependent`, not just under `collection.delete` (which I found much later). Briefly mention the broader impacts of `:dependent` in the Association Basics guide. [ci skip] --- guides/source/association_basics.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index f75517c5ab..822dee08f0 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -1662,6 +1662,8 @@ Controls what happens to the associated objects when their owner is destroyed: * `:restrict_with_exception` causes an `ActiveRecord::DeleteRestrictionError` exception to be raised if there are any associated records * `:restrict_with_error` causes an error to be added to the owner if there are any associated objects +The `:destroy` and `:delete_all` options also affect the samantics of the `collection.delete` and `collection=` methods by causing them to destroy associated objects when they are removed from the collection. + ##### `:foreign_key` By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix `_id` added. The `:foreign_key` option lets you set the name of the foreign key directly: -- cgit v1.2.3