thoughtbot / ios-on-rails

A guide to building a Rails API and iOS app
Other
76 stars 6 forks source link

Geocoder thowing a PG error #68

Closed edwardloveall closed 10 years ago

edwardloveall commented 10 years ago

For anyone going through the book, on the very last test in the rails section, (sad path geocoder), you may get this error:

ActiveRecord::StatementInvalid:
       PG::SyntaxError: ERROR:  syntax error at or near "AS"
       LINE 1: ...-122.430782 - events.lon) * PI() / 180 / 2), 2))) AS distanc...
                                                                    ^
       : SELECT COUNT(events.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((37.771098 - events.lat) * PI() / 180 / 2), 2) + COS(37.771098 * PI() / 180) * COS(events.lat * PI() / 180) * POWER(SIN((-122.430782 - events.lon) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((events.lon - -122.430782) / 57.2957795), ((events.lat - 37.771098) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing) FROM "events"  WHERE (events.lat BETWEEN 37.76210478394081 AND 37.78009121605919 AND events.lon BETWEEN -122.44215913432566 AND -122.41940486567432 AND (6371.0 * 2 * ASIN(SQRT(POWER(SIN((37.771098 - events.lat) * PI() / 180 / 2), 2) + COS(37.771098 * PI() / 180) * COS(events.lat * PI() / 180) * POWER(SIN((-122.430782 - events.lon) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND '1')

It's a bug that has to do with the implementation of empty? which .any? uses. They're aware of it over on the Geocoder gem issues.

A hacky work around is to use: if @events.count(:all) > 0 instead of if @events.any?

jessieay commented 10 years ago

Hi Edward - that should be fixed in the latest version of the book. someone else reported this issue earlier and I thought I fixed it. Can you confirm? 

Happy Memorial Day! — Sent from my phone

On Mon, May 26, 2014 at 5:30 PM, Edward Loveall notifications@github.com wrote:

For anyone going through the book, on the very last test in the rails section, (sad path geocoder), you may get this error:

ActiveRecord::StatementInvalid:
       PG::SyntaxError: ERROR:  syntax error at or near "AS"
       LINE 1: ...-122.430782 - events.lon) * PI() / 180 / 2), 2))) AS distanc...
                                                                    ^
       : SELECT COUNT(events.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((37.771098 - events.lat) * PI() / 180 / 2), 2) + COS(37.771098 * PI() / 180) * COS(events.lat * PI() / 180) * POWER(SIN((-122.430782 - events.lon) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((events.lon - -122.430782) / 57.2957795), ((events.lat - 37.771098) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing) FROM "events"  WHERE (events.lat BETWEEN 37.76210478394081 AND 37.78009121605919 AND events.lon BETWEEN -122.44215913432566 AND -122.41940486567432 AND (6371.0 * 2 * ASIN(SQRT(POWER(SIN((37.771098 - events.lat) * PI() / 180 / 2), 2) + COS(37.771098 * PI() / 180) * COS(events.lat * PI() / 180) * POWER(SIN((-122.430782 - events.lon) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND '1')

It's a bug that has to do with the implementation of empty? which .any? uses. They're aware of it over on the Geocoder gem issues.

A hacky work around is to use: if @events.count(:all) > 0 instead of if @events.any?

Reply to this email directly or view it on GitHub: https://github.com/thoughtbot/ios-on-rails/issues/68

edwardloveall commented 10 years ago

Ah ha! Looks like you did fix this in a555e267, but I don't think it made it to the books. I'm reading the HTML one specifically but it's also not in the epub, md, or pdf. I didn't check the mobi but I'd assume it's not in there either.

Proud of my self that I came up with the same fix! :clap:

jessieay commented 10 years ago

Ah yes, I haven't done a new build of the book since then. My bad! Will do so now. :)