指定单表继承的子类映射

时间:2017-06-06 12:04:55

标签: ruby-on-rails ruby inheritance

The documentation继承解释了如何轻松实施STI,因为您可以选择type列的值。

但是,我有一个遗留数据库,这些值已经存在。假设这些是PRIORITYECONOMYFLEXI,我如何指定如何将这些值映射到子类?

class PriorityPassenger < Passenger
  # map to PRIORITY
end

1 个答案:

答案 0 :(得分:1)

这应该有效(未经测试):

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)
相关问题