An ActiveRecord plugin for self-referential and double-sided polymorphic associations.
Copyright 2006-2008 Cloudburst, LLC. Licensed under the AFL 3. See the included LICENSE file.
The public certificate for the gem is here.
This plugin lets you define self-referential and double-sided polymorphic associations in your models. It is an extension of
“Polymorphic” means an association can freely point to any of several unrelated model classes, instead of being tied to one particular class.
efficient database usage
automatic individual and reverse associations
The plugin also includes a generator for a tagging system, a common use case (see below).
Rails 2.2.2 or greater
To install the Rails plugin, run:
script/plugin install git://github.com/fauna/has_many_polymorphs.git
There's also a gem version. To install it instead, run:
sudo gem install has_many_polymorphs
If you are using the gem, make sure to add
require 'has_many_polymorphs' to
environment.rb, before Rails::Initializer block.
Setup the parent model as so:
class Kennel < :: has_many_polymorphs :guests, :from => [:dogs, :cats, :birds] end
The join model:
class GuestsKennel < :: belongs_to :kennel belongs_to :guest, :polymorphic => true end
One of the child models:
class Dog < :: # nothing end
For your parent and child models, you don't need any special fields in your migration. For the join model (GuestsKennel), use a migration like so:
class CreateGuestsKennels < ::Migration def self.up create_table :guests_kennels do |t| t.references :guest, :polymorphic => true t.references :kennel end end def self.down drop_table :guests_kennels end end
See ActiveRecord::Associations::PolymorphicClassMethods for more configuration options.
Helper methods example
>> k = Kennel.find(1) #<Kennel id: 1, name: "Happy Paws"> >> k.guests.map(&:class) [Dog, Cat, Cat, Bird] >> k.guests.push(Cat.create); k.cats.size 3 >> k.guests << Cat.create; k.cats.size 4 >> k.guests.size 6 >> d = k.dogs.first #<Dog id: 3, name: "Rover"> >> d.kennels [#<Kennel id: 1, name: "Happy Paws">] >> k.guests.delete(d); k.dogs.size 0 >> k.guests.size 5
Note that the parent method is always plural, even if there is only one parent (
See ActiveRecord::Associations::PolymorphicAssociation for more helper method details.
Double-sided relationships are defined on the join model:
class Devouring < :: belongs_to :guest, :polymorphic => true belongs_to :eaten, :polymorphic => true acts_as_double_polymorphic_join( :guests =>[:dogs, :cats], :eatens => [:cats, :birds] ) end
Now, dogs and cats can eat birds and cats. Birds can't eat anything (they aren't
guests) and dogs can't be eaten by anything (since they aren't
eatens). The keys stand for what the models are, not what they do.
In this case, each guest/eaten relationship is called a Devouring.
In your migration, you need to declare both sides as polymorphic:
class CreateDevourings < ::Migration def self.up create_table :devourings do |t| t.references :guest, :polymorphic => true t.references :eaten, :polymorphic => true end end def self.down drop_table :devourings end end
See ActiveRecord::Associations::PolymorphicClassMethods for more.
Has_many_polymorphs includes a tagging system generator. Run:
script/generate tagging Dog Cat [...MoreModels...]
This adds a migration and new Tag and Tagging models in
app/models. It configures Tag with an appropriate
has_many_polymorphs call against the models you list at the command line. It also adds the file
requires it in
Tests will also be generated.
Once you've run the generator, you can tag records as follows:
>> d = Dog.create(:name => "Rover") #<Dog id: 3, name: "Rover"> >> d.tag_list "" >> d.tag_with "fierce loud" #<Dog id: 3, name: "Rover"> >> d.tag_list "fierce loud" >> c = Cat.create(:name => "Chloe") #<Cat id: 1, name: "Chloe"> >> c.tag_with "fierce cute" #<Cat id: 1, name: "Chloe"> >> c.tag_list "cute fierce" >> Tag.find_by_name("fierce").taggables [#<Cat id: 1, name: "Chloe">, #<Dog id: 3, name: "Rover">]
The generator accepts the optional flag
--skip-migration to skip generating a migration (for example, if you are converting from
acts_as_taggable). It also accepts the flag
--self-referential if you want to be able to tag tags.
See ActiveRecord::Base::TaggingExtensions, Tag, and Tagging for more.
Some debugging tools are available in
If you are having trouble, think very carefully about how your model classes, key columns, and table names relate. You may have to explicitly specify options on your join model such as
:as. The included tests are a good place to look for examples.
Note that because of the way Rails reloads model classes, the plugin can sometimes bog down your development server. Set
config.cache_classes = true in
config/environments/development.rb to avoid this.
The support forum is here.
Patches and contributions are very welcome. Please note that contributors are required to assign copyright for their additions to Cloudburst, LLC.