alexreisner / alexreisner/geocoder
Unexpected behavior with .empty?
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 6.4k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
Rails 4.1.0.rc2, Geocoder 1.2.1, Ruby 2.0.0p451, Postgres 9.3.3.0
When making a query with .near, calling .empty? on the returned collection results in an SQL syntax error. Taking any intervening action using the collection between instantiating it and calling .empty? on it will prevent the error.
I believe this is likely related to the issue listed with .count on the readme page, given the nature of the feedback from the console. If it is, perhaps a work around that is less hacky than the one I use below could be suggested in the readme.
Example (using an app I'm building):
@users = User.near("129 West 81st St. New York, NY", 20)
@users.empty?
Running this code produces the following query:
SELECT COUNT(users.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((40.783871 - users.latitude) * PI() / 180 / 2), 2) + COS(40.783871 * PI() / 180) * COS(users.latitude * PI() / 180) * POWER(SIN((-73.97535599999999 - users.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((users.longitude - -73.97535599999999) / 57.2957795), ((users.latitude - 40.783871) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing) FROM "users" WHERE (users.latitude BETWEEN 40.4944074337783 AND 41.073334566221696 AND users.longitude BETWEEN -74.35764813847217 AND -73.59306386152781 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((40.783871 - users.latitude) * PI() / 180 / 2), 2) + COS(40.783871 * PI() / 180) * COS(users.latitude * PI() / 180) * POWER(SIN((-73.97535599999999 - users.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 20)
and the following syntax error:
PG::SyntaxError: ERROR: syntax error at or near "AS"
LINE 1: ...999999 - users.longitude) * PI() / 180 / 2), 2))) AS distanc...
Doing anything with the object in between like puts @users will break the chain and cause .empty? to behave as expected.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with User.near(...) followed immediately by .empty?, using the Rails, Geocoder, Ruby, and PostgreSQL versions listed. Compare the generated COUNT SQL with the behavior after an intervening collection action. Done means .empty? runs without the PostgreSQL syntax error and preserves the expected empty/non-empty result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, rails, ruby
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100