Posted about 6 years ago. Visible to the public.

Use custom type name with Ruby on Rails STI mechanism.

Let's say you want to modify default type column Rails uses to identify class -> subclass inheritance with the same database table. Just use the following methods:

Copy
class User < ActiveRecord::Base end class Admin < User def find_sti_class(type_name) type_name = self.name super end def sti_name 'Administrator' end end

Now you can see that in table users you have type 'Administrator' which transforms to class Admin inside Rails ActiveRecord.

Owner of this card:

Avatar
Alexander M
Last edit:
about 6 years ago
by Alexander M
Posted by Alexander M to Ruby and RoR knowledge base
This website uses short-lived cookies to improve usability.
Accept or learn more