Skip to content

Instantly share code, notes, and snippets.

@minhajuddin
Forked from sumskyi/README.md
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minhajuddin/5e50a211e4529dec20b8 to your computer and use it in GitHub Desktop.
Save minhajuddin/5e50a211e4529dec20b8 to your computer and use it in GitHub Desktop.

irb(main):029:0* GeoEntity.last

GeoEntity Load (0.5ms) SELECT geo_entities.* FROM geo_entities ORDER BY geo_entities.id DESC LIMIT 1

returns:

County id: 4, eid: nil, pid: nil, ename: nil, etype: 2, created_at: "2011-11-21 06:26:37", updated_at: "2011-11-21 06:26:37"

class City < GeoEntity
end
class County < GeoEntity
end
class GeoEntity < ActiveRecord::Base
set_inheritance_column :etype
ENTITIES = {
1 => GeoEntity::State,
2 => GeoEntity::County,
3 => GeoEntity::City,
4 => GeoEntity::Zip
}
class << self
def find_sti_class(type_name)
ENTITIES[type_name.to_i] or super
end
def sti_name
ENTITIES.invert[self]
end
end
end
class State < GeoEntity
end
class Zip < GeoEntity
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment