# Annoying find thingy

**URL:** <https://www.ruby-forum.com/t/annoying-find-thingy/134549>\
**Category:** Rails\
**Created:** [April 11, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/annoying-find-thingy/134549 "2008-04-11T21:34:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Col\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Col\_W](https://www.ruby-forum.com/u/Col_W)\
**Post date:** [April 11, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/annoying-find-thingy/134549/1 "2008-04-11T21:34:12Z")

</div>

I want to know which thingy is centered on a given city. Both thingy  
and city have a lat and a lng attribute, ad lat and lng are both  
floats in the mysql database.

knowing how clever ActiveRecord::Base is I wanted to do:

City.find\_by\_lat\_and\_lng(self.lat, self.lng)

but that interprets lat and lng as

SELECT \* FROM `cities` WHERE (`cities`.`lat` = ‘50.12’ AND  
`cities`.`lng` = ‘-5.7’) LIMIT 1

and since lat/lng are floats that won’t work,

I would have thought that

City.find(:first, :conditions =\> [ “lat = ? AND lng = ?”, self.lat,  
self.lng ])

SELECT \* FROM `cities` WHERE (lat = 50.13 AND lng = -5.56) LIMIT 1

would work but it returns nothing.

what I had to do was

City.find(:first, :conditions =\> [ “lat LIKE ? AND lng LIKE ?”,  
self.lat, self.lng ])

which becomes

SELECT \* FROM `cities` WHERE (lat LIKE 50.13 AND lng LIKE -5.56)  
LIMIT 1

and works.

Surely the first case is an ActiveRecord::Base problem in that it  
ought to interpret a float as a float and not a string (LIKE), the  
second is a mysql problem in that the query is good SQL and the third  
is just wrong, although it does work.

---

<div class="post-metadata">

**Author:** ![Col\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Col\_W](https://www.ruby-forum.com/u/Col_W)\
**Post date:** [April 11, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/annoying-find-thingy/134549/2 "2008-04-11T21:48:59Z")

</div>

float just does not play well with ‘==’, however represented. You  
nearly always need to provide some form of tolerance to allow for the  
inherent imprecision of  
the internal representation. Given the application, you probably need  
to provide some circular probability of error regarding fixing the  
spherical co-ordinate centre in any case.

---

<div class="post-metadata">

**Author:** ![Col\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Col\_W](https://www.ruby-forum.com/u/Col_W)\
**Post date:** [April 11, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/annoying-find-thingy/134549/3 "2008-04-11T23:15:06Z")

</div>

Col,

Take a peek at GeoKit: [http://geokit.rubyforge.org/](http://geokit.rubyforge.org/)

BTW, use Decimal (BigDecimal?) for your lats & lngs. Better precision.  
To wit, I have an Addresses table declared as follows:

```
create_table :addresses, :force => true do |t|

```

t.string :full\_address, :street\_address, :city, :state, :zip, :country  
t.decimal :lat, :lng, :precision =\> 15, :scale =\> 10  
end

Works great for me. GeoKit lets you do all the lookup goodness,  
including some nice “find” overrides.

-Danimal

On Apr 11, 1:33 pm, Col W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Col\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Col\_W](https://www.ruby-forum.com/u/Col_W)\
**Post date:** [April 12, 2008, 4:03am UTC](https://www.ruby-forum.com/t/annoying-find-thingy/134549/4 "2008-04-12T04:03:27Z")

</div>

Not sure if it applied to what you’re doing, but it seems like you’d  
have a tolerance issue even without the imprecision of the float. My  
city is roughly 40N 86W, but that wouldn’t find it in an ‘=’ query.  
Maybe a between query with a fudge factor, like:

:conditions =\> [“lat between ? and ? and lon between ? and ?”,lat-  
fudge,lat+fudge,lon-fudge,lon+fudge]

To keep it DRY, you could build your own finder method on city.

-Mack

On Apr 11, 8:09 pm, Col W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Col\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Col\_W](https://www.ruby-forum.com/u/Col_W)\
**Post date:** [April 12, 2008, 2:10am UTC](https://www.ruby-forum.com/t/annoying-find-thingy/134549/5 "2008-04-12T02:10:02Z")

</div>

Thanks for that.
