Adding a custom Postgresql Type with Rails 4.2

In one of my Rails applications, we are using the PostGIS custom Postgres data type GEOGRAPHY. When ActiveRecord stumbles across this data type, by default it does not know what to do with it.

Prior to Rails 4.1, there was a utility method on the ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID module called register_type.

In Rails 4.2, this method was removed, so there is no way to easily register a new type.

To get around this problem, I decided to monkey patch PostgreSQLAdapter.



ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
  def initialize_type_map_with_postgres_oids(m)
    initialize_type_map_without_postgres_oids(m)
    register_class_with_limit m, 
      'geography', 
      ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Integer
  end
  alias_method_chain :initialize_type_map, :postgres_oids
end