Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Geokit to return closest record returns PG::SyntaxError #71

Open
samlester opened this issue Mar 4, 2015 · 1 comment
Open

Using Geokit to return closest record returns PG::SyntaxError #71

samlester opened this issue Mar 4, 2015 · 1 comment

Comments

@samlester
Copy link

I'm using Geokit to find the closest station to a location. The code runs after validation.

Here's the Location model:

class Location < ActiveRecord::Base
  belongs_to :station

  # Find nearest station after validation
  after_validation :update_nearest_station

  def update_nearest_station
    nearest_station = Station.closest(:origin => [latitude,longitude])

    update_attribute(:station_id, nearest_station.id)
  end
end

and the Station model:

class Station < ActiveRecord::Base
  has_many :locations

  acts_as_mappable :default_units => :km, :lat_column_name => :latitude, :lng_column_name => :longitude
end

When I run the code either by editing/adding a new Location or through my seed data I get the following error:

ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near ")"

LINE 4: ... SIN(0.8992918053979032)*SIN(RADIANS(stations.latitude))))*)
                                                                  ^
: SELECT  "stations".* FROM "stations"  ORDER BY 
      (ACOS(least(1,COS(0.8992918053979032)*COS(-0.0014597933863680574)*COS(RADIANS(stations.latitude))*COS(RADIANS(stations.longitude))+
      COS(0.8992918053979032)*SIN(-0.0014597933863680574)*COS(RADIANS(stations.latitude))*SIN(RADIANS(stations.longitude))+
      SIN(0.8992918053979032)*SIN(RADIANS(stations.latitude))))*)
      asc LIMIT 1

I've tried lots of different ways of doing this using Geokit's other methods (e.g. within with first) but I can't seem to get it to work.

Is this a bug with Geokit or am I missing something really obvious?

@mbaylet
Copy link

mbaylet commented Feb 6, 2017

For those wondering it's related to the unit.
If you set the :default_units to an unknow value the request failed (for my case :km instead of :kms)
Related to #97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants