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


Fixing unknown OID: geography errors with PostGis and Rails 4.0

Recently, when migrating a large Rails application to Rails 4.0 that used PostGis, I kept getting warnings like: unknown OID: geography(1838124) Thanks to http://gray.fm/2013/09/17/unknown-oid-with-rails-and-postgresql/ I just added a file config/initializers/postgres_oids.rb with content: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.tap do |klass| # Use identity which does no casting klass::OID.register_type(‘geography’, klass::OID::Identity.new) end


ActiveRecord default_scope is an Anti-Pattern

TL;DR Active Record’s default_scope should be renamed to something like always_prepend_scope to better describe what the method does. The Inspiration The other day, "default_scope" is an anti-pattern came across my twitter feed.  Having been burned by this issue I get the sentiment. The Problem with default_scope Let’s say I have a simple class like the […]


Solr Sorting Tidbit

The basic requirement was a fuzzy search on a series of business names and locations using a Levenshtein fuzzy string to handle typos, with results being returned with those whose name started with the query term displayed first followed by others that may have matched.  For our purposes, let’s say I had the following data […]


Viewing the command line using top

So I was watching what was happening on one of my Linux boxes the other day using top. I meant to quit using Ctrl-c, but instead, I missed the control key and just hit ‘c’. To my wonder, the list changed and now I could see the full command line for each process. How crazy […]