I extensively updated my polymorphic has_many :through association helper plugin, and it’s worth a second look. You now get snazzy
_delete() methods on the polymorphic collection:
petfood = Petfood.find(1) petfood.eaters.map(&:class) # => [Dog, Cat, Cat, Bird] petfood.eaters_push(Cat.create) petfood.reload petfood.cats.size # => 3 petfood.eaters.size # => 5 petfood.eaters_delete(petfood.dogs) petfood.reload petfood.dogs.size # => 0 petfood.eaters.size # => 4 # works both ways petfood.eaters.petfoods.include?(petfood) # => true
And configuration is easier than even regular ActiveRecord:
class Petfood < ActiveRecord::Base has_many_polymorphs :eaters, :from => [:dogs, :cats, :birds] end class EatersPetfood < ActiveRecord::Base belongs_to :petfood belongs_to :eater, :polymorphic => true end class Dog < ActiveRecord::Base end
For more information and the plugin itself, go here.