garci560
garci560

Reputation: 3213

Specify subclass mapping for Single Table Inheritance

The documentation for inheritance explains how to easily implement STI given that you can choose the values for the type column.

However, I have a legacy DB, and these values already exist. Let's say those are PRIORITY, ECONOMY, and FLEXI, how do I specify how to map these values to subclasses?

class PriorityPassenger < Passenger
  # map to PRIORITY
end

Upvotes: 0

Views: 217

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

This should work (untested):

ActiveRecord::Inheritance::ClassMethods.prepend(Module.new do
  def find_sti_class(type_name)
    case type_name
    when 'PRIORITY' then PriorityPassenger
    ...
    else super(type_name)
    end
  end
end)

Upvotes: 1

Related Questions