-
-
Save vamdt/75aca125883a88a9f1fd to your computer and use it in GitHub Desktop.
rails4 sti, custom "type" column name and value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class City < GeoEntity | |
def self.sti_name | |
3 | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Country < GeoEntity | |
def self.sti_name | |
2 | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GeoEntity < ActiveRecord::Base | |
self.inheritance_column = 'etype' | |
def self.find_sti_class(type_name) | |
self | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class State < GeoEntity | |
def self.sti_name | |
1 | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Zip < GeoEntity | |
def self.sti_name | |
4 | |
end | |
end |
π Just what I was looking for
π Good stuff!
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?
@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
π