【问题标题】:Using Geokit to return closest record returns PG::SyntaxError使用 Geokit 返回最近的记录返回 PG::SyntaxError
【发布时间】:2015-06-14 03:37:46
【问题描述】:

我正在使用 Geokit 来查找离某个位置最近的车站。验证后代码运行。

这是位置模型:

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

和 Station 模型:

class Station < ActiveRecord::Base
  has_many :locations

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

当我通过编辑/添加新位置或通过我的种子数据运行代码时,我收到以下错误:

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

我使用 Geokit 的其他方法(例如 withinfirst)尝试了很多不同的方法,但我似乎无法让它工作。

这是 Geokit 的错误还是我遗漏了一些非常明显的东西?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.2 geokit


    【解决方案1】:

    单位的值可以是 :miles、:kms(公里)或 :nms(航海 英里),使用 :miles 作为默认值。

    所以::default_units =&gt; :kms

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      相关资源
      最近更新 更多