-
-
Save AlexKVal/3126103575b6df8f20533b5408fd0821 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) | |
type_name = self.name # so GeoEntity.new(etype: 2) returns Country | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment