This project is archived and is in readonly mode.

#6441 new
Russell Garner

polymorphic :dependent => :delete_all fails to delete rows with abstract_class instances

Reported by Russell Garner | February 16th, 2011 @ 11:03 AM

If I have a class hierarchy ContactIndex < Index, GuideIndex < Index, where Index is

  class Index < ActiveRecord::Base
    has_many :links, :as => :link_owner, :dependent => :delete_all

and Link is

  class Link < ActiveRecord::Base
    belongs_to :link_owner, :polymorphic => true

When I instantiate and save a GuideIndex, the links table gets a row with a link_owner_type of Index. When I subsequently delete the index, the links rows are orphaned:

  i =
  i << => 'http://somewhere')    # This creates links row with link_owner_type = 'Index'
  i.destroy # This tries to delete rows with link_owner_type = 'GuideIndex' and finds no rows

At present, my workaround is this:

  class Index < ActiveRecord::Base
    has_many :links, :as => :link_owner
    before_destroy :delete_links

    def delete_links
      Link.delete_all ['links.link_owner_id = ? AND links.link_owner_type = ?',, 'Index']

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket