STI with legacy table

I maintain a rails app which uses a lot of data from a commercial
system (a student records system) and adds much functionality.

I’m having to make some changes to make the system work with upcoming
changes to their database and have run into a situation where using
single table inheritance would be the perfect solution.

The only problem is that the column I need to use stores the object
type as a single letter instead of a whole class name. Is there a way
I can use STI with a mapping for type column? So that the string
stored in the type column is not the class name, but maps 1-to-1 to
one?

Thanks in advance

Kevin Hughes

I guess you can. Consider this code.

class Category < ActiveRecord::Base
end

class Subcategory < Category
end

I am inheriting Subcategory from Category. The table’s column ‘type’
will have values either ‘Category’ or ‘Subcategory’. If I change
‘Subcategory’ to ‘A’ then my ‘type’ column is going to have ‘Category’
or ‘A’

for ex.

class A < Category
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs