Back to all posts
3 min read

Finding the Road Back Home: Building a Clean PostGIS Gem for Rails 8

Finding the Road Back Home: Building a Clean PostGIS Gem for Rails 8

Finding the Road Back Home: Building a Clean PostGIS Gem for Rails 8 Or: How I Finally Stopped Having Monkeys in My Codebase

After years of wrestling with spatial data in Rails, my codebase had become a literal zoo—complete with monkeys swinging through monkey patches, chaotic adapter inheritance trees, and compatibility disasters that made upgrades feel like playing Jenga with gorillas. But I’ve finally escaped the zoo by creating activerecord-postgis, a clean, Rails 8-friendly PostGIS adapter gem. Here’s the real story behind it.

The Problem: Welcome to the Jungle

If you’ve handled spatial data in Rails, you know the pain:

  • Monkey Patches Everywhere: ActiveRecord was mutated beyond recognition.
  • Inheritance Chaos: Adapter hierarchies tangled beyond belief.
  • Module Madness: Gems battling each other through aggressive method prepends.
  • Upgrade Nightmares: Rails upgrades—from 6.1 to 8—left everything broken.

My own code was a guilty example:

# Monkey patch central
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
  include SpatialMixin
  prepend YetAnotherSpatialPatch

  alias_method :old_create_table, :create_table

  def create_table(*)
    # Terrifying spatial logic
    old_create_table(*)
  end
end

Yes, it worked—if you ignored the fragility and constant fear.

Rails 8: Déjà Vu All Over Again

When Rails 8 hit, upgrading was brutally hard—just like Rails 6.1 had been. Each weekend I spent wrestling monkeys felt increasingly wasted. I realized: why keep patching when I could rebuild?

Rebuilding: Not as Simple as it Sounds

Starting fresh didn’t mean I’d just rename st_point to spatial_pt and watch users suffer. Sure, I love clean code, but developers need stability and clear APIs—not surprises from my whims.

With AI shaping documentation, naming APIs oddly is a fast route to frustration. OSS developers (myself included) work at His Majesty’s pleasure—unpaid, unsponsored, no fancy funding. Writing docs was tedious, but now, with AI, I just jot down ideas and let ChatGPT, Claude, or Gemini handle formatting. It’s like hosting “AI’s Got Talent,” voting on which version makes the semifinals.

The New Way: Rails-Friendly and Stable

The new gem, activerecord-postgis, offers:

1. Rails-Compatible Extensions

PostgreSQLAdapter.prepend(PostGIS::AdapterExtensions)
ActiveRecord::Type.register(:st_point, Type::Point, adapter: :postgresql)

ActiveSupport.on_load(:active_record_postgresqladapter) do
  ActiveRecord::ConnectionAdapters::PostGIS.initialize!
end

2. Column Methods Done Right

# Clear and precise migrations
create_table :locations do |t|
  t.st_point :location, geographic: true
  t.st_polygon :boundary, geographic: true
end

3. Fully Integrated Rails Type System

class Point < Spatial
  def initialize(srid: 0, has_z: false, has_m: false, geographic: false)
    super(geo_type: "point", srid: srid, has_z: has_z, has_m: has_m, geographic: geographic)
  end
end

4. Reliable Schema Dumping

No more schema mysteries:

create_table "locations", force: :cascade do |t|
  t.st_point "location", geographic: true
  t.st_polygon "boundary", geographic: true, srid: 4326
end

A Note on the Past

The old adapters weren’t inherently bad—they were forks of the standard PostgreSQL adapter with spatial types. I myself participated in early monkey patching—not due to lack of skill, but because ActiveRecord wasn’t mature enough then to handle such mutations. No bashing the original devs; they did their best with what they had.

Lessons Learned (Again, the Hard Way)

  • Work With Rails: Going against Rails always makes life harder.
  • Stable APIs Matter: Exotic naming frustrates users and AI tools.
  • AI Helps Documentation: Writing docs is now dramatically easier.
  • Real-World Testing: Gems should thrive in actual apps, not just isolated tests.

The Road Ahead

Future plans include:

  • Advanced PostGIS features
  • Performance enhancements
  • Richer documentation (AI-assisted, naturally)
  • Spatial data validations

Coming Home

After endless monkey-patching, returning to clean code feels liberating. If you’re managing spatial data in Rails, embrace the new, stable approach—your future self (and sanity) will thank you.


activerecord-postgis is ready on GitHub—clean, stable, monkey-free.

Yes, this article is redacted with AI assistance—not generated. Not because I’m farming views, but because English is not my native language.

Related Posts