Understanding Rails Polymorphic Associations

Featured image

Polymorphic associations allow a model to belong to more than one other model using a single association. This is particularly useful when you want a single model to be associated with multiple other models without having to create multiple associations or tables.

Use Case Scenario

Imagine you have three models: Post, Image, and Comment. You want to allow users to comment on both Post and Image. Instead of creating separate associations for Post and Image in the Comment model, you can use a polymorphic association.

Models Setup

  1. Post Model:
    class Post < ApplicationRecord
      has_many :comments, as: :commentable
    end
    
  2. Image Model:
    class Image < ApplicationRecord
      has_many :comments, as: :commentable
    end
    
  3. Comment Model:
    class Comment < ApplicationRecord
      belongs_to :commentable, polymorphic: true
    end
    

Database Schema

The comments table will have two additional columns: commentable_type and commentable_id. These columns allow the Comment model to be associated with both the Post and Image models.

create_table :comments do |t|
  t.text :body
  t.references :commentable, polymorphic: true, index: true
  t.timestamps
end

Usage In Action

Creating a comment for a Post:

post = Post.find(1)
comment = post.comments.create(body: "Great post!")

Creating a comment for an Image:

image = Image.find(1)
comment = image.comments.create(body: "Nice image!")

Diagram

Here’s a simple diagram to illustrate how polymorphic associations work:

+------------------+           +----------------+
|      Post        |           |      Image      |
|------------------|           |----------------|
| id: integer      |           | id: integer     |
| title: string    |           | url: string     |
| ...              |           | ...             |
+------------------+           +----------------+
         |                          |
         |                          |
         |                          |
         |                          |
         |                          |
         |            +-------------+-----------+
         |            | Comment                   |
         |            |--------------------------|
         +----------->| id: integer               |
                      | body: text                |
                      | commentable_id: integer   |
                      | commentable_type: string  |
                      +--------------------------+

Explanation

Benefits of Polymorphic Associations

  1. Flexibility: You can associate multiple models with a single model.
  2. Reduced Redundancy: Instead of creating separate associations or tables, you manage relationships using a single interface.
  3. Simplified Schema: Only one polymorphic association is needed instead of multiple foreign keys.

Conclusion

Polymorphic associations are a powerful feature in Rails that allow for more flexible and DRY (Don’t Repeat Yourself) code. They are particularly useful in scenarios where a model needs to interact with multiple other models in a similar way, like comments, tags, or likes.