Search code examples
ruby-on-railspostgresqlpolymorphic-associations

Destroy orphaned polymorphic association in Active Record


I recently created a notifications system using a polymorphic association in my Rails application. All was working well until the system stopped showing notifications for certain users. I realized the culprit was an orphaned notification - where Notification.notifiable_id was returning a non-existent user. Here's a diagram:

Notification is created on Comment creation

User model:

has_many Notifications, dependent: destroy

So there is still the possibility that The user responsible for creating the notification can delete their account, leaving the Notification record orphaned on one end.

Questions: 1. How do I delete all notifications where Notification.notifiable_id is not found?

  1. How do I prevent this issue from breaking the Notifications system in the future?

I realized after posting this I had misunderstood the polymorphic association. I had actually set up Notification.notifiable to point back to a Comment created by a User, not the User itself.

I fixed issue #2 by modifying the Comment model (as well as other models acting as 'notifiable')

    has_many :notifications, as: :notifiable, dependent: :destroy

Solution

    1. For your data fix

      Notification.all.each do |notification|
        notification.destroy if notification.notifiable.nil?
      end
      
    2. Looks like you are missing a colon on your dependent: :destroy. This would have prevented your Notifications from being properly destroyed when a User record is destroyed.