I extensively updated my polymorphic has_many :through association helper plugin, and it’s worth a second look. You now get snazzy _push()
and _delete()
methods on the polymorphic collection:
example
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[0])
petfood.reload
petfood.dogs.size # => 0
petfood.eaters.size # => 4
# works both ways
petfood.eaters[0].petfoods.include?(petfood) # => true
And configuration is easier than even regular ActiveRecord:
usage
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.
Yer blog layout is a bit messed up. The post content is displayed at the top of the page above your page header.
Fixed? I think you visited within the 3 minutes I was moving stylesheets around.
Thanks though.