Along with Bill Eisenhauer, I’ve been digging into what it would take to fix the problem I found with GeoKit, that it wouldn’t support :include queries properly. The explanation has seen us going deep into the internals of ActiveRecord to discover, as others have before, that the :select parameter (which GeoKit uses to build its distance column in the query) and the :include parameter don’t play nicely together.
The reason for that is that Rails employs special methods to build the select parameters when there’s a :include in the query (ie. when you’re joining with other tables and eager-loading them) and currently ignores :select along the way. GeoKit would ordinarily produce SQL along the lines of:
SELECT *, SQRT( POW(69.1*(42.962927-latitude), 2) + POW(42.0590342306803*(-85.637179-longitude), 2) ) AS distance FROM bus_stops ORDER BY distance ASC LIMIT 1
but with a :include on a has_many relationship, we don’t get that distance column and so most of the queries will fail.