# Find\_by\_sql not working

**URL:** <https://www.ruby-forum.com/t/find-by-sql-not-working/144989>\
**Category:** Rails\
**Created:** [August 5, 2008, 12:08am UTC](https://www.ruby-forum.com/t/find-by-sql-not-working/144989 "2008-08-05T00:08:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [August 5, 2008, 12:08am UTC](https://www.ruby-forum.com/t/find-by-sql-not-working/144989/1 "2008-08-05T00:08:44Z")

</div>

I have the following complex query:

locations = Location.find\_by\_sql(“SELECT \* FROM locations join  
mediaGeography on locations.zip = mediaGeography.geoZip WHERE  
(((acos(sin(( #{coords[“geoLAT”]} \* pi()/180)) \* sin((  
mediaGeography.geoLAT \* pi()/180)) + cos(( #{coords[“geoLAT”]} \*  
pi()/180)) \* cos((mediaGeography.geoLAT \* pi()/180)) \*  
cos(((#{coords[“geoLON”]} -mediaGeography.geoLON ) \*  
pi()/180))))\*180/pi())_60_1.1515) \< #{ distance }”)

Obviously messy. I _DO NOT_ have a “mediaGeography” model but there is a  
table in my DB called “mediaGeography”. Anyway this returns 0  
“locations” when it should return 15. If I go to ‘development.log’ file  
and copy and paste the generated query there directly to mysql it does  
return the right number of columns, i.e. 15 and not 0.

Any ideas?

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [August 5, 2008, 2:46am UTC](https://www.ruby-forum.com/t/find-by-sql-not-working/144989/2 "2008-08-05T02:46:10Z")

</div>

I have an idea. If you call a  
Model.connection.select\_one("…")

before, do I need to call some kind of SQL close before moving on to the  
next query?

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [August 5, 2008, 3:00am UTC](https://www.ruby-forum.com/t/find-by-sql-not-working/144989/3 "2008-08-05T03:00:24Z")

</div>

On Aug 5, 1:46 am, Allen W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have an idea. If you call a  
> Model.connection.select\_one(“…”)
> 
> before, do I need to call some kind of SQL close before moving on to the  
> next query?

Nope. Does it make any difference if you change select \* to select  
locations.\* (on the off chance that you’ve got an attribute name there  
which clashes with an activerecord/ruby function ?

Fred
