PG::SyntaxError at / ERROR: syntax error at or near “)” Rails

别等时光非礼了梦想. 提交于 2019-12-11 20:11:36

问题


I am getting this error some time and some time not. That is strange.

ERROR: syntax error at or near ")" LINE 1: SELECT locations.*, () as distance FROM "locations" ORDER ...

In line

nearby_locations.includes(:events).each do |location|

In my Dashboard controller

 def home            
    nearby_locations = Location.with_distance_to(remote_ip).order('distance').limit(10)
    @events  = []
    nearby_locations.includes(:events).each do |location|
        @events += location.events.where("publish = true")
    end
end

In my Location.rb model

geocoded_by :address
extend Geocoder::Model::ActiveRecord
  reverse_geocoded_by :latitude, :longitude

  scope :with_distance_to, ->(point) { select("#{table_name}.*").select("(#{distance_from_sql(point)}) as distance") }

回答1:


To stop the errors, you should validate that remote_ip in your dashboard_controller exists before using the scope you have defined on Location.

 def home            
  unless remote_ip.blank?
    nearby_locations = Location.with_distance_to(remote_ip).order('distance').limit(10)
    @events  = []
    nearby_locations.includes(:events).each do |location|
        @events += location.events.where("publish = true")
    end
  end
end

To really understand the problem, find out how remote_ip is blank in the first place and fix that issue.



来源:https://stackoverflow.com/questions/33244132/pgsyntaxerror-at-error-syntax-error-at-or-near-rails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!