Discover how one simple association can save you from endless repetitive code!
Why Polymorphic associations in Ruby on Rails? - Purpose & Use Cases
Imagine you have a blog app where comments can belong to posts, photos, or events. You try to create separate comment tables for each type manually.
Managing multiple comment tables means repeating code, complex queries, and lots of confusion when fetching or adding comments. It's slow and error-prone.
Polymorphic associations let you use one comments table that can belong to many different models. Rails handles the connections smoothly and cleanly.
class PostComment < ApplicationRecord; belongs_to :post; end class PhotoComment < ApplicationRecord; belongs_to :photo; end
class Comment < ApplicationRecord; belongs_to :commentable, polymorphic: true; end class Post < ApplicationRecord; has_many :comments, as: :commentable; end class Photo < ApplicationRecord; has_many :comments, as: :commentable; end class Event < ApplicationRecord; has_many :comments, as: :commentable; end
You can easily add comments to any model without extra tables or complex code, making your app flexible and maintainable.
A social media app where users can comment on posts, photos, and videos all using the same comments system.
Manual separate tables for each association cause duplication and complexity.
Polymorphic associations unify related data in one place.
Rails automates linking models, saving time and reducing bugs.
Practice
Solution
Step 1: Understand polymorphic association concept
Polymorphic associations let one model belong to multiple other models through a single interface.Step 2: Compare options with concept
To allow a model to belong to more than one other model using a single association correctly describes this flexibility. Other options describe unrelated features.Final Answer:
To allow a model to belong to more than one other model using a single association -> Option AQuick Check:
Polymorphic association = flexible belongs_to [OK]
- Thinking it creates one-to-one relationships
- Confusing with database foreign key constraints
- Assuming it auto-generates views
Solution
Step 1: Recall belongs_to polymorphic syntax
The correct syntax usesbelongs_to :name, polymorphic: truein the model that belongs to multiple types.Step 2: Match options with syntax
belongs_to :commentable, polymorphic: true matches this exactly. Options A and D use wrong association types, and C uses incorrect keyword.Final Answer:
belongs_to :commentable, polymorphic: true -> Option DQuick Check:
belongs_to with polymorphic: true = belongs_to :commentable, polymorphic: true [OK]
- Using has_many instead of belongs_to for polymorphic
- Using 'as: :polymorphic' instead of 'polymorphic: true'
- Confusing has_one with belongs_to
class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true end class Post < ApplicationRecord has_many :comments, as: :commentable end class Photo < ApplicationRecord has_many :comments, as: :commentable end
What will
post.comments.first.commentable_type return if the first comment belongs to a Post?Solution
Step 1: Understand polymorphic type storage
The polymorphic association stores the class name of the associated object incommentable_type.Step 2: Identify the associated model
The comment belongs to a Post, socommentable_typewill be "Post".Final Answer:
"Post" -> Option CQuick Check:
commentable_type stores class name = "Post" [OK]
- Confusing commentable_type with comment class
- Expecting the associated object instead of its type string
- Assuming nil if association exists
class Picture < ApplicationRecord belongs_to :imageable, polymorphic: true end
Which of these will cause an error when creating a Picture?
Solution
Step 1: Check required polymorphic keys
Polymorphic belongs_to requires bothimageable_typeandimageable_idto be present for a valid association.Step 2: Identify missing or nil values
Picture.create(imageable_type: nil, imageable_id: 2) hasimageable_type: nil, which breaks the association and causes an error.Final Answer:
Picture.create(imageable_type: nil, imageable_id: 2) -> Option AQuick Check:
Missing polymorphic type causes error [OK]
- Assuming nil imageable_type is acceptable
- Ignoring missing type causes runtime error
- Confusing polymorphic keys with normal foreign keys
Solution
Step 1: Identify polymorphic association pattern
Comments belong to a polymorphiccommentableinterface, allowing multiple models to share comments.Step 2: Match correct model declarations
class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true end class Article < ApplicationRecord has_many :comments, as: :commentable end class Event < ApplicationRecord has_many :comments, as: :commentable end correctly usesbelongs_to :commentable, polymorphic: truein Comment andhas_many :comments, as: :commentablein Article and Event.Final Answer:
polymorphic belongs_to and has_many as: :commentable -> Option BQuick Check:
Polymorphic belongs_to + has_many as: = class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true end class Article < ApplicationRecord has_many :comments, as: :commentable end class Event < ApplicationRecord has_many :comments, as: :commentable end [OK]
- Trying to belongs_to both models separately
- Using has_many in Comment instead of belongs_to
- Assigning belongs_to comment in parent models
Ready to prove it?
Pass the test and this pattern moves unknown
