Question

it sounds like not difficult at all but after looking for a while I can't find a way to change the limit of the closest scope in Geokit-rails.

I've tried to overide the definition of this method which is defined according to the documentation as:

    def closest(options = {})
       geo_scope(options).order("#{distance_column_name} asc").limit(1)
    end

but with no success.

Any idea? Thank you very much!

Was it helpful?

Solution

That's an older version of geokit-rails/geokit, now the definition is:

def closest(options = {})
  by_distance(options).first(1)
end

So no need to override anything, your code could simply say:

MyClass.by_distance(options).limit(my_limit)

Note: Try to avoiding monkey patching existing code. If you did what was suggested in another comment it might work now, but as soon as you update geokit/geokit-rails you risk it breaking as implementations do change (e.g. the closest method is implemented rather differently.

OTHER TIPS

You can override this method, something like:

 module Geokit
   def closest(options = {})
     geo_scope(options).order("#{distance_column_name} asc").limit(options[:limit] || 1)
   end
 end

and put this file in config/initializers folder and restart server. Note: put correct module name, classes. Consider it like a path to method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top