One of the most powerful aspects of Ruby on Rails are the associations we can create between two classes. It is immensely convenient to be able to call person.posts rather than doing a SQL statement to find all of the posts with a person_id of X.

Sometimes, we have instances where the associations could be with multiple classes. For example, if we have a Page class that can be associated with an HTMLAdvertisement or an ImageAdvertisement. In that case, we really want to be able to call @page.advertisement to get the advertisement. This is where polymorphic associations come in.

Polymorphic associations allow us to associate a single attribute of the class to any number of specified classes. Here are the Page, HtmlAdvertisement, and ImageAdvertisement models:

1
2
3
class Page < ActiveRecord::Base
  belongs_to :advertisement, :polymorphic => true
end
1
2
3
class HtmlAdvertisement < ActiveRecord::Base
  has_many :pages, :as => :advertisement
end
1
2
3
class ImageAdvertisement < ActiveRecord::Base
  has_many :pages, :as => :advertisement
end

Pretty simple example, but you’re probably wondering how this works in the database. After all, an attribute of advertisement_id won’t cut it as both the image_advertisements table and the html_advertisements table will both start their id’s at 0 and don’t reference each other when incrementing.

This is where the Rails convention over configuration comes in. The polymorphic association looks for an [attribute]_type and an [attribute]_id field on the pages table.

Here’s our migration:

1
2
3
4
5
6
7
8
9
10
11
12
13
class CreatePages < ActiveRecord::Migration
  def self.up
    create_table :pages do |t|
      t.string :advertisement_type
      t.integer :advertisement_id
      # ... other fields for the pages table
    end
  end
 
  def self.down
    drop_table :pages
  end
end

As I’ve stated before, I recommend that you put your polymorphic associations and related functionality into mixin modules.

If you’re using restful routes and would like to use the rails helpers to generate links to polymorphic resources where you might not know the class, here’s a helpful article on how to do that.

Finally, here’s a link to the documentation written by the Rails team.

  • DZone
  • Twitter
  • Slashdot
  • Delicious
  • Digg
  • Technorati Favorites
  • Facebook
  • Reddit
  • StumbleUpon
  • LiveJournal
  • Squidoo
  • Google Bookmarks
  • LinkedIn
  • Share/Bookmark

3 Comments »

Twitter Trackbacks for Ruby on Rails: What are Polymorphic Associations? [charlesmaxwood.com] on Topsy.com

August 21, 2009

[...] Ruby on Rails: What are Polymorphic Associations? charlesmaxwood.com/ruby-on-rails-what-are-polymorphic-associations – view page – cached An explanation on what polymorphic associations are and how to use them. — From the page [...]

Ennuyer.net » Blog Archive » Rails Reading - August 26, 2009

August 26, 2009

[...] Ruby on Rails: What are Polymorphic Associations? [...]

Ashish Roy

September 24, 2009

Nice to have info, thanks

Leave a comment