Added customizable STI type serialization. #7541

Closed
wants to merge 1 commit into
from
Commits on Sep 6, 2012
  1. Added customizable STI type serialization.

    Ian Lesperance & Matt Parker committed with elliterate Sep 5, 2012
    Rails will store the name of a child class in the database
    to use for re-instantiating the record as the correct class later.
    By specifying an `inheritance_serializer` and
    `inheritance_deserializer`, you can customize the stored identifier.
    
        class Foo < ActiveRecord::Base
          self.inheritance_serializer = ->(klass) do
            # Map the class to the appropriate type identifier.
            # Defaults to `klass.name`.
          end
    
          self.inheritance_deserializer = ->(type_before_cast) do
            # Map the type identifier back into the appropriate class.
            # Defaults (approximately) to `type_before_cast.constantize`.
          end
        end
    
    This is primarily useful for working with legacy data models.