Skip to content

Instantly share code, notes, and snippets.

@vamdt vamdt/city.rb forked from sumskyi/README.md
Last active Mar 14, 2019

Embed
What would you like to do?
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
@adis-io

This comment has been minimized.

Copy link

adis-io commented May 20, 2015

👍

@jjasonclark

This comment has been minimized.

Copy link

jjasonclark commented Jun 25, 2015

👍 Just what I was looking for

@konradrr

This comment has been minimized.

Copy link

konradrr commented Feb 27, 2016

👍 Good stuff!

@vizo

This comment has been minimized.

Copy link

vizo commented Jul 23, 2016

I did GeoEntity.new({etype: 1}) and

def self.find_sti_class(type_name)
    self
end

this is returning base class and not subclass? Is this correct or i did something wrong?

@mpressen

This comment has been minimized.

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
You can’t perform that action at this time.