# Please critique this geo scope

**URL:** <https://discuss.elastic.co/t/please-critique-this-geo-scope/5607>\
**Category:** Elasticsearch\
**Created:** [October 15, 2011, 3:42pm UTC](https://discuss.elastic.co/t/please-critique-this-geo-scope/5607 "2011-10-15T15:42:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alan/32/3105_2.png) [@Alan](https://discuss.elastic.co/u/Alan)\
**Post date:** [October 15, 2011, 3:42pm UTC](https://discuss.elastic.co/t/please-critique-this-geo-scope/5607/1 "2011-10-15T15:42:48Z")

</div>

I'm just wondering if this looks alright or if it might be bad  
practice. Aim is to be able to do a simple geo search as if it were  
any regular scope:

class Place \< ActiveRecord::Base  
...  
scope :near, Proc.new { |latitude, longitude, options|  
options = options && options.dup || {}  
options.reverse\_merge!(:range =\> 1)  
where(:id =\> Tire.search('places') do  
filter 'geo\_distance', :distance =\> "#{options[:range]}  
km", :location =\> [latitude, longitude]  
end.results.collect(&:id))  
}  
end

Seems to work. So I could do this for example:

Place.near(51.8481, -8.4794, :range =\>  
10).where(...).joins(...).includes(...) # other regular Arel stuff

Look alright or is there a proper way?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 17, 2011, 2:43am UTC](https://discuss.elastic.co/t/please-critique-this-geo-scope/5607/2 "2011-10-17T02:43:14Z")

</div>

Not a ruby/rails expert, but looks good to me. Karmi?

On Sat, Oct 15, 2011 at 5:42 PM, Alan [alan@iorum.ie](mailto:alan@iorum.ie) wrote:

> I'm just wondering if this looks alright or if it might be bad  
> practice. Aim is to be able to do a simple geo search as if it were  
> any regular scope:
> 
> class Place \< ActiveRecord::Base  
> ...  
> scope :near, Proc.new { |latitude, longitude, options|  
> options = options && options.dup || {}  
> options.reverse\_merge!(:range =\> 1)  
> where(:id =\> Tire.search('places') do  
> filter 'geo\_distance', :distance =\> "#{options[:range]}  
> km", :location =\> [latitude, longitude]  
> end.results.collect(&:id))  
> }  
> end
> 
> Seems to work. So I could do this for example:
> 
> Place.near(51.8481, -8.4794, :range =\>  
> 10).where(...).joins(...).includes(...) # other regular Arel stuff
> 
> Look alright or is there a proper way?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:51am UTC](https://discuss.elastic.co/t/please-critique-this-geo-scope/5607/3 "2017-07-06T03:51:46Z")

</div>

Closed on July 6, 2017, 3:51am UTC


