---
title: "Changing a polymorphic_type in Rails"
description: "Rails stores the parent's class name in a polymorphic association's type column, so moving models into a new namespace (e.g. Car to Garage::Car) leaves mixed values like \"Car\" and \"Garage::Car\" and breaks lookups in both directions."
url: "https://swe.quest/articles/changing-a-polymorphic-type-in-rails-8b5924e77ba397de"
canonical: "https://shopify.engineering/changing-polymorphic-type-rails"
source: "Shopify Engineering"
published: "2022-02-18"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["ruby","refactoring","schema-migrations","databases"]
---

# Changing a polymorphic\_type in Rails

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2022-02-18, 7 min read, How-to. Original: [https://shopify.engineering/changing-polymorphic-type-rails](https://shopify.engineering/changing-polymorphic-type-rails)

## Summary

Rails stores the parent's class name in a polymorphic association's type column, so moving models into a new namespace (e.g. Car to Garage::Car) leaves mixed values like "Car" and "Garage::Car" and breaks lookups in both directions. Shopify's Payment Flexibility team decoupled the database from class names by overriding polymorphic\_name to write arbitrary strings such as "car", then used an unscoped association scope to read both old and new values during the transition. After a backfill of existing rows, the temporary scope is removed and future renames only require updating a class-mapping hash. The approach adds complexity and is mainly worth it when module and class names are expected to change.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Refactoring & migrations](https://swe.quest/tags/refactoring.md) (Engineering practices)
- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- Rails Active Record associations ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- polymorphic associations

## Keywords

Ruby on Rails, Active Record, polymorphic\_name, MaintenanceTasks

## Related posts

- [Caching Without Marshal Part 1: Marshal from the Inside Out](https://swe.quest/articles/caching-without-marshal-part-1-marshal-from-the-inside-out-57f413ca18174072.md): Shopify Engineering · 2022-11-11 · Medium · 11 min read · Ruby, Caching, Runtimes & VMs
  Ruby's Marshal serializes an object's full class graph into cache entries, so a refactor that renamed classes caused old and new code to choke on each other's cached beta-flag objects mid-deploy and triggered an incident at Shopify.
- [How GitHub converts previously encrypted and unencrypted columns to ActiveRecord encrypted columns](https://swe.quest/articles/how-github-converts-previously-encrypted-and-unencrypted-columns-to-7a6a32eb2ff23c82.md): GitHub Engineering · 2022-11-03 · Medium · 7 min read · Ruby, Cryptography, Schema migrations, Security, Deployment
  GitHub migrated plaintext columns and columns encrypted with a deprecated internal library to ActiveRecord::Encryption.
- [Caching Without Marshal Part 2: The Path to MessagePack](https://swe.quest/articles/caching-without-marshal-part-2-the-path-to-messagepack-dde2d14188f0b673.md): Shopify Engineering · 2022-11-15 · Medium · 17 min read · Ruby, Caching, Refactoring & migrations
  Shopify replaced Ruby's Marshal with MessagePack as the Rails cache format for its core monolith so that code changes would stop breaking cached payloads on deploy.
- [Declarative Schemas for Simpler Database Management](https://swe.quest/articles/declarative-schemas-for-simpler-database-management-ee2428d0ae8c51e8.md): Supabase · 2025-04-03 · Medium · 4 min read · Schema migrations, PostgreSQL, Developer tools
  Supabase CLI now supports declarative schemas: the desired final state of Postgres tables, views, functions, RLS policies, grants and types lives in versioned .sql files, and a schema diff tool such as migra generates the migration files.
- [Focus On Behavior, Not State, for a More Maintainable Codebase](https://swe.quest/articles/focus-on-behavior-not-state-for-a-more-maintainable-codebase-d186fd1356391a16.md): Shopify Engineering · 2022-09-01 · Medium · 9 min read · Refactoring & migrations, Ruby, E-commerce, System design
  When adding pre-orders and try-before-you-buy to Shopify's Purchase Options APIs, code that equated "has a selling plan" with "is a subscription" forced repeated refactors.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
