avatarDuy Nguyen

Summary

In Ruby on Rails, polymorphic associations allow a model to associate with multiple other models, while delegated types (or Single Table Inheritance) use a single table to represent multiple models with shared attributes and a type column to distinguish between them.

Abstract

Polymorphic associations in Ruby on Rails provide a flexible way for a model to be associated with more than one other model using a single association, which is particularly useful when multiple models need to be associated with a single model without creating separate associations for each. On the other hand, delegated types, also known as Single Table Inheritance (STI), involve using one table to store records for multiple models that share common attributes but may also have unique attributes or behaviors, differentiated by a 'type' column. This approach is beneficial when dealing with several models with similarities but also specific differences. The choice between polymorphic associations and delegated types depends on the specific needs of the application, considering factors such as maintainability and performance.

Opinions

  • Polymorphic associations are recommended when the associated models do not share common attributes or behaviors.
  • Delegated types are suitable for models with shared attributes or behaviors but with some distinct differences.
  • The use of a type column in delegated types allows Rails to automatically handle the differentiation between model types.
  • It is important to weigh the trade-offs of each approach, as they can have significant implications for the application's maintainability and performance.
  • The decision on which technique to use should be based on the application's specific requirements and the nature of the relationships between models.
Photo by Sigmund on Unsplash

Polymorphic associations vs delegated types in Rails

In Ruby on Rails, both polymorphic associations and delegated types are techniques used to establish relationships between different models in a flexible and dynamic way. However, they serve different purposes and are used in different scenarios. Let’s explore the differences between them:

Polymorphic Associations

Polymorphic associations allow a model to belong to more than one other model on a single association. This is often used when you have multiple models that need to be associated with a single model, without having to create a separate association for each related model.

For example, let’s say you have a Comment model that can be associated with either a Post or an Article. Instead of creating separate post_id and article_id columns in the Comment table, you can use a polymorphic association.

In Rails, a polymorphic association is established using the belongs_to association with the polymorphic: true option, and the related models use the has_many association with the as: :<relation_name> option.

Delegated Types

Delegated types (also known as Single Table Inheritance or STI) involve using a single table to store multiple models with shared attributes. Each model is differentiated by a type column in the table. This can be useful when you have several models that share common attributes and behaviors, but also have some distinct attributes or behaviors.

For instance, if you have a Notification model and you want to have different types of notifications (e.g., CommentNotification, LikeNotification, etc.), you can use a delegated type approach. The Notification model would have a type column that determines the specific type of notification.

Rails handles this automatically when you use a type column in the table and create subclasses of a base model for each specific type of notification.

In summary:

  • Polymorphic Associations: Used when you want a model to be associated with multiple other models through a single association. Helpful when the associated models have no shared attributes or behaviors.
  • Delegated Types (STI): Used when you have multiple models that share some common attributes or behaviors but also have distinct attributes or behaviors. All models share a single table with a type column to differentiate between them.

Choose the technique that best fits your application’s needs and the relationships you want to establish between your models. Always consider the trade-offs and implications of each approach for the maintainability and performance of your application.

References: https://api.rubyonrails.org/classes/ActiveRecord/DelegatedType.html#method-i-delegated_type https://guides.rubyonrails.org/association_basics.html#polymorphic-associations

Ruby on Rails
Ruby
Recommended from ReadMedium