Performance: Polymorphic associations
MEDIUM IMPACT
Polymorphic associations affect database query speed and server response time, impacting how fast pages load data.
class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true end # In controller @comments = Comment.includes(:commentable).all @comments.each do |comment| puts comment.commentable.title end
class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true end # In controller @comments = Comment.all @comments.each do |comment| puts comment.commentable.title end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| N+1 queries on polymorphic associations | Minimal DOM nodes | 0 reflows | Low paint cost | [X] Bad |
| Eager loading polymorphic associations | Minimal DOM nodes | 0 reflows | Low paint cost | [OK] Good |