Skip to content

kmerz/polymorphic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Polymorphic
===========

Polymorphic is an alternative implementation of polymorphic associations
in Rails.
Instead of using one join table which includes all associations, there
is one join table for each associations.

Polymorphic works for has_many and has_one associations.

To use polymorphic edit config/application.rb:
  config.plugins = [ :polymorphic ]
And included to the file in question via:
  require 'polymorphic'


Example
=======

In the example we have three classes:
  class Car < ActiveRecord::Base
    has_and_belongs_to_many :streets, :join_table => "street_cars"
  end

  class Bike < ActiveRecord::Base
    has_and_belongs_to_many :streets, :join_table => "street_bikes"
  end

  class Street < ActiveRecord::Base
    has_and_belongs_to_many :cars, :join_table => "street_cars"
    has_and_belongs_to_many :bikes, :join_table => "street_bikes"
    polymorphic :vehicles, :cars, :bikes
  end

The has_many associations of Cars and Bikes in Class Street are in
need of join tables.
They are regular has_many associations as known to Rails.

  polymorphic :vehicles, :cars, :bikes
will add a PolyProxy which just behaves like an AssociationProxy providing
an interface to the connected has_many associations:
  s = Street.find(1)
  c = Car.first
  s.vehicles << c

Copyright (c) 2012 genua mbh Kirchheim bei Muenchen, released under the MIT license

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages