From e464d06696e402ae725cbb9d638d4aa1070c4d9b Mon Sep 17 00:00:00 2001 From: Philip Nguyen Date: Fri, 19 Aug 2016 15:49:57 -0700 Subject: Doc on scoped has_many, dependent: :destroy This is to close #26111 Developers need to be aware that `dependent: :destroy` on a scoped `has_many` association would only destroy the associated objects in that scope. Potentially leaving other associated objects outside that scope untouched. --- activerecord/lib/active_record/associations.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib/active_record/associations.rb') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 659e512ce2..dc68b01386 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1296,6 +1296,12 @@ module ActiveRecord # If using with the :through option, the association on the join model must be # a #belongs_to, and the records which get deleted are the join records, rather than # the associated records. + # + # If using dependent: :destroy on a scoped association, only the scoped objects are destroyed. + # For example, if a Post model defines + # has_many :comments, -> { where published: true }, dependent: :destroy and destroy is + # called on a post, only published comments are destroyed. This means that any unpublished comments in the + # database would still contain a foreign key pointing to the now deleted post. # [:counter_cache] # This option can be used to configure a custom named :counter_cache. You only need this option, # when you customized the name of your :counter_cache on the #belongs_to association. -- cgit v1.2.3