alexreisner / alexreisner/geocoder

Unexpected behavior with .empty?

Open
#652 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

ruby/rails change
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.