0
0
Ruby on Railsframework~10 mins

Polymorphic associations in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Polymorphic associations
Define polymorphic model
Add polymorphic reference to other models
Create records linking to different models
Access associated records via polymorphic interface
Use polymorphic methods to get related objects
Shows how a model can belong to multiple other models through a single interface using polymorphic references.
Execution Sample
Ruby on Rails
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

class Post < ApplicationRecord
  has_many :comments, as: :commentable
end

class Image < ApplicationRecord
  has_many :comments, as: :commentable
end
Defines a Comment model that can belong to different models like Post or Image using polymorphic association.
Execution Table
StepActionEvaluationResult
1Create Post recordPost.create(title: 'Hello')Post with id=1 created
2Create Comment linked to PostComment.create(body: 'Nice post', commentable: Post#1)Comment with id=1 linked to Post#1
3Create Image recordImage.create(name: 'Pic')Image with id=1 created
4Create Comment linked to ImageComment.create(body: 'Great image', commentable: Image#1)Comment with id=2 linked to Image#1
5Access commentable of Comment#1Comment.find(1).commentableReturns Post#1
6Access commentable of Comment#2Comment.find(2).commentableReturns Image#1
7Access comments of Post#1Post.find(1).commentsReturns Comment#1
8Access comments of Image#1Image.find(1).commentsReturns Comment#2
9ExitNo more actionsPolymorphic links established and accessible
💡 All polymorphic associations created and verified
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Post#1nilPost(id=1, title='Hello')Post(id=1, title='Hello')Post(id=1, title='Hello')Post(id=1, title='Hello')Post(id=1, title='Hello')Post(id=1, title='Hello')
Image#1nilnilnilImage(id=1, name='Pic')Image(id=1, name='Pic')Image(id=1, name='Pic')Image(id=1, name='Pic')
Comment#1nilnilComment(id=1, body='Nice post', commentable=Post#1)Comment(id=1, body='Nice post', commentable=Post#1)Comment(id=1, body='Nice post', commentable=Post#1)Comment(id=1, body='Nice post', commentable=Post#1)Comment(id=1, body='Nice post', commentable=Post#1)
Comment#2nilnilnilnilComment(id=2, body='Great image', commentable=Image#1)Comment(id=2, body='Great image', commentable=Image#1)Comment(id=2, body='Great image', commentable=Image#1)
Key Moments - 3 Insights
Why does Comment belong_to :commentable instead of a specific model like Post?
Because :commentable is polymorphic, Comment can link to different models (Post, Image, etc.) using the same interface, as shown in steps 2 and 4 where comments link to different models.
How does Rails know which model a comment belongs to?
Rails uses two columns: commentable_type (model name) and commentable_id (record id). This is why in step 5 accessing commentable returns the correct model instance.
Can we access comments from both Post and Image models?
Yes, because both models declare has_many :comments, as: :commentable, so steps 7 and 8 show accessing comments from different models.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does Comment#1's commentable refer to at step 5?
AImage#1
BPost#1
CComment#2
Dnil
💡 Hint
Check step 5 in the execution_table where Comment#1's commentable is accessed.
At which step is a Comment linked to an Image created?
AStep 4
BStep 6
CStep 2
DStep 8
💡 Hint
Look for the step where Comment.create links to Image#1 in the execution_table.
If we add a new model Video with comments, which part of the flow changes?
ADefine polymorphic model
BCreate records linking to different models
CAdd polymorphic reference to Video
DAccess associated records
💡 Hint
Adding a new model requires adding polymorphic references to that model, as shown in the concept_flow step 2.
Concept Snapshot
Polymorphic associations let a model belong to multiple other models through one interface.
Use belongs_to :name, polymorphic: true in the polymorphic model.
Other models use has_many :name, as: :name to link.
Rails stores type and id to track the association.
Access related objects via the polymorphic interface transparently.
Full Transcript
Polymorphic associations in Rails allow one model to belong to more than one other model using a single association. For example, a Comment can belong to a Post or an Image. This is done by adding belongs_to :commentable, polymorphic: true in the Comment model. The Post and Image models then declare has_many :comments, as: :commentable. When creating comments, Rails stores the type of the associated model and its id. This lets you access the associated object easily, like comment.commentable returning either a Post or Image instance. This approach avoids creating separate associations for each model and keeps code flexible and clean.