Technology and Software

Geokit and Rails3: ArgumentError and undefined method merge_conditions

I had to use Geokit in a Rails 3 application and I run into a number of problems.

I was using the geokit gem so I got the undefined method merge_conditions error as soon as I used a geokit extension into a ActiveRecord method. That’s true, merge_conditions has been removed from ActiveRecord::Base version 2.3.9 and geokit hasn’t been updated to work around that. Somebody monkey patched merge_conditions back into it (see this post on stackoverflow) but that’s not the right way to go with Rails 3.

Luckily there is a new version of the gem for Rails 3, a fork from the original gem. I tried to use it in the way I would have used the old one but I had ArgumentError instead of undefined method merge_conditions. Not a big improvement but after googling around and some trial and error I found a solution. This is the recipe:

Add gem “geokit-rails3” into the Gemfile. Do a bundle install.

Use geokit like this

def index
  bounds = Geokit::Bounds.new(
             Geokit::LatLng.new(params[:swlat],
                                params[:swlng]),
             Geokit::LatLng.new(params[:nelat],
                                params[:nelng]))
  @points = MyModel.geo_scope(:bounds => bounds)
end

The combination of Geokit::Bounds.new, Geokit::LatLng.new and MyModel.geo_scope was the solution. Passing the params directly to geo_scope didn’t work.

Advertisements
Standard