# Has\_finder and Geokit

**URL:** <https://www.ruby-forum.com/t/has-finder-and-geokit/132066>\
**Category:** Rails\
**Created:** [March 16, 2008, 9:15am UTC](https://www.ruby-forum.com/t/has-finder-and-geokit/132066 "2008-03-16T09:15:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuelzx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@samuelzx](https://www.ruby-forum.com/u/samuelzx)\
**Post date:** [March 16, 2008, 9:15am UTC](https://www.ruby-forum.com/t/has-finder-and-geokit/132066/1 "2008-03-16T09:15:10Z")

</div>

I have recently discovered the brilliant has\_finder. It allows you to  
name your ActiveRecord searches,  
and stack them together. It fulfills a similar need to scope\_out.

One of my searches is location based, using the excellent plugin  
Geokit.  
[http://geokit.rubyforge.org/](http://geokit.rubyforge.org/). Normally I can do a spatial search like  
so:

MyModel.find(:all, :origin =\> [lat, lng], :within =\> miles\_in\_int)

When I try to do this within a has\_finder finder (see below), it  
complains  
:origin and :within can not be find. It must be accessing the finder  
methods  
directly without going through Geokit.

has\_finder :proximity, lambda {|lat, lon| { :origin =\> [lat,  
lon], :within  
=\> 100 } }

normal has\_finder  
has\_finder :of\_type, lambda {|type\_id| { :conditions =\>  
{:feature\_type\_id =\>  
type\_id} } }

I am not sure how it all works under the hood. Can anyone advice on  
getting this to work? Any pointers would really help.

I’ll take a look at the source code in the mean time.

Thanks,  
Xin

---

<div class="post-metadata">

**Author:** ![samuelzx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@samuelzx](https://www.ruby-forum.com/u/samuelzx)\
**Post date:** [June 4, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/has-finder-and-geokit/132066/2 "2008-06-04T20:16:05Z")

</div>

It would be nice if this would work. I’m using GeoKit with named\_scope  
in Rails 2.1.

---

<div class="post-metadata">

**Author:** ![samuelzx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@samuelzx](https://www.ruby-forum.com/u/samuelzx)\
**Post date:** [November 8, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/has-finder-and-geokit/132066/3 "2008-11-08T21:03:20Z")

</div>

Mark J.on wrote:

> It would be nice if this would work. I’m using GeoKit with named\_scope  
> in Rails 2.1.

I got this working and it was really easy.

Models with “acts\_as\_mappable” have a method for distance\_sql(origin,  
units=default\_units, formula=default\_formula). I used this to create my  
condition.

I am using anonymous scopes in a Search model which provides search for  
an Organization model, but a similar technique should work with  
named\_scope. See this Railscast on creating a search with anonymous  
scopes: [#112 Anonymous Scopes - RailsCasts](http://railscasts.com/episodes/112-anonymous-scopes)

I have “acts\_as\_mappable” on both my Search model and on my Organization  
model. This allows me to build a scope condition like this:

scope = scope.conditions “#{Organization.distance\_sql(self)} \<  
#{self.distance}”

Note that “self” is the search in this case.

I hope this helps.

BB
