0

I have an existing table bases with a column sub_type where the values of sub_type match to some existing classes that I want to use for Single Table Inheritance, but the class names don't exactly match the values in the column.

For example, I have:

class Base

class SubClass1 < Base # stored in 'sub_type' as 'sub_class_1' instead of 'SubClass1'

class SubClass2 < Base # stored in 'sub_type' as 'sub_class_2' instead of 'SubClass2' 

Is there a way to get rails to correctly map between the name 'sub_class_1' and the class 'SubClass1' without renaming the classes or rewriting the values in the database?

  • Does this answer your question? [How to customize Rails 3 STI column name](https://stackoverflow.com/questions/9454595/how-to-customize-rails-3-sti-column-name) – jamesc Aug 17 '20 at 23:02
  • 1
    No, I do not need to change the column name. Instead I need to change the value stored in the column. – Alan Savage Aug 18 '20 at 20:52

1 Answers1

0

You can play with following

class User < ActiveRecord::Base
  self.store_full_sti_class = false
  ...
end

I have not tried it. I have just referred to method definition of https://apidock.com/rails/v5.2.3/ActiveRecord/Inheritance/ClassMethods/sti_name

Anand Bait
  • 331
  • 1
  • 12