Skip to content

Instantly share code, notes, and snippets.

@vamdt
Forked from sumskyi/README.md
Last active July 26, 2022 05:01
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vamdt/75aca125883a88a9f1fd to your computer and use it in GitHub Desktop.
Save vamdt/75aca125883a88a9f1fd to your computer and use it in GitHub Desktop.
rails4 sti, custom "type" column name and value
class City < GeoEntity
def self.sti_name
3
end
end
class Country < GeoEntity
def self.sti_name
2
end
end
class GeoEntity < ActiveRecord::Base
self.inheritance_column = 'etype'
def self.find_sti_class(type_name)
self
end
end
class State < GeoEntity
def self.sti_name
1
end
end
class Zip < GeoEntity
def self.sti_name
4
end
end
@mpressen
Copy link

mpressen commented Mar 14, 2019

@vizo yes it doesn't return subclass.
My code for a lowercase string in 'type' with expected behaviour :

class Super < ApplicationRecord

  def self.sti_name
    name.downcase
  end

  def self.find_sti_class(type_name)
    type_name.classify.constantize
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment