Polymorphic Associations With Active Record

An exceptionally productive web application framework, Ruby on Rails is optimized for programmer delight and sustainable output. It favours convention over configuration and thus lets you write beautiful code without worrying too much about the conformation.

There is an increasing demand for Ruby on Rails because of its speed and agility in building applications with improved productivity and reduced delivery time.

Active Record Associations are one of the most important features of Rails. Polymorphic association is part of these associations. With polymorphic association, a model can belong to more than one other model, on a single association.

Why do we need associations between models? Because they make common operations simpler and easier in your code. With Active Record associations, we can streamline these – and other – operations by declaratively telling Rails that there is a connection between the two models.

In Rails, an association is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model belongs_to another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails support six types of associations:

Let’s see how to implement it. First we will need to create Document model.

rails g model Document name:string wpl_article_id:integer


class Document < ActiveRecord::Base

belongs_to :wpl_article, :polymorphic => true


The Interface:

Using a polymorphic association, we need to define only a single belongs_to and add a pair of related columns to the underlying database table. From that moment on, any class in our system can have documents attached to it (which would make it wpl_article) without needing to alter the database schema or the Document model itself.

There isn’t a wpl_article class (or module) in our application. We named the association :wpl_article because it accurately describes the interface of objects that will be associated in this way. The name

:wpl_article will turn up again on the other side of the association:

class Software < ActiveRecord::Base

has_one :document, :as => :wpl_article


class Hardware < ActiveRecord::Base

has_one :document, :as => :wpl_article


class Research < ActiveRecord::Base

has_one :document, :as => :wpl_article


The Database Columns: (association_name)_type – Stores the type for

polymorphic associations.

class CreateDocuments < ActiveRecord::Migration

def change

create_table :documents do |t|

t.string :name

t.integer :wpl_article_id # as we have mentioned “belongs_to

:wpl_article, :polymorphic => true” in document model . This is a

convention of schema that association_name_type and

association_name_id will be accordingly “wpl_article”.

t.string :wpl_article_type




Software, Hardware and Research has one document via polymorphic association wpl_article Here we can see how model. Document is associated with three model Software, Hardware and Research in single association via wpl_article which is helping in achieving the association.

Loading development environment (Rails 4.1.1)

2.0.0-p247 :001 > document = Document.create(name: ‘Worldcup)

=> #<Document id: 1, wpl_article_type: nil, wpl_article_id: nil, name:


2.0.0-p247 :002 > software = Software.create(name: ‘cricinfo.com’)

=> #<Software id: 1, name: “cricinfo.com”>

2.0.0-p247 :003 > document.update_attribute(:wpl_article, software)

=> #<Document id: 1, wpl_article_type: ‘Software’, wpl_article_id: 1,

name: “Worldcup”>

=> true

2.0.0-p247 :004 > Software.last.document.name

=> “Worldcup”

2.0.0-p247 :005 >Document.last

=> #<Document id: 1, wpl_article_type: nil, wpl_article_id: nil, name:


2.0.0-p247 :006 >Document.last.wpl_article

it will fetch the recoed based on wpl_article_id and wpl_article_type

Railscarma provides end-to-end Ruby on Rails solutions from development, deployment, management, monitoring and supporting your apps.

Working on Ruby’s application from its nascent stages, RailsCarma gives you the advantage of its pioneering expertise to help you gain prompt and simplified business solutions.


Get in touch with us.

Leave a Comment

Your email address will not be published. Required fields are marked *