BlogsShopifyPolymorphic Association Storage Strategy

Polymorphic Association Storage Strategy

Polymorphic Association Storage Strategy

1
posts
2022

This feature thread tracks the development and enhancement of strategies for managing polymorphic associations in Rails, focusing on reducing coupling between the codebase and the database. Initial efforts addressed the challenges of namespace changes breaking polymorphic associations. This post details a solution to stop storing class names as polymorphic types, instead using arbitrary strings. This allows for easier refactoring of class names and namespaces without database migrations. The solution involves overriding the `polymorphic_name` method and adjusting association scopes to handle both old and new storage formats during the transition, followed by a cleanup task to standardize on arbitrary strings.

2022

Changing a polymorphic_type in Rails - Shopify

2/18/2022

Introduced a technique to change how Rails persists polymorphic associations by replacing class names with arbitrary strings. This involves overriding the `polymorphic_name` method and using `.unscope` on associations to support both old and new storage formats during a transition period, followed by a database cleanup task. This reduces coupling between the codebase and the database, facilitating future refactoring of class names and namespaces.