This project is archived and is in readonly mode.
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
end
and Link
is
class Link < ActiveRecord::Base
belongs_to :link_owner, :polymorphic => true
end
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 = GuideIndex.new
i << Link.new(:url => 'http://somewhere')
i.save # 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
private
def delete_links
Link.delete_all ['links.link_owner_id = ? AND links.link_owner_type = ?', self.id, 'Index']
end
end
Comments and changes to this ticket
-
Russell Garner February 16th, 2011 @ 11:20 AM
(oops -
abstract_class
is not involved here - the title should read STI. Sorry!)
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>