rails/rails — Issue #5093: has_many polymorphic with dependent destroy not always cleaning up
Author: KieranP
Created: 2012-02-19 23:33:39+00:00
State: open

I have the following code:

``` ruby
module HasActivity
def self.included(klass)
klass.extend ClassMethods
end
module ClassMethods
def has_activity
has_many :activities, :as => :target, :dependent => :destroy
end
end
end

class Activity
belongs_to :target, :polymorphic => true
end

class Comment
has_activity
end
```

Sometimes (not always but frequently enough to be annoying), when a comment is deleted, it leaves behind the activity items. The target_type and target_id are still populated on the activity records, but point to a non-existant comment.

I've been unable to trace the cause for the issue, and was hoping I could get some discussions going as to why this might be happening.