0

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") }
Adt
  • 495
  • 6
  • 19
  • 2
    If you run `distance_from_sql(point)` in your model, what does it return? – yez Oct 20 '15 at 18:30
  • @yez how to do that ? – Adt Oct 21 '15 at 14:22
  • It is a method on your `Location` model. You should be able to do `Location.distance_from_point(some_point)` where `some_point` is a point, the same that you were passing to `with_distance_to` scope. – yez Oct 21 '15 at 17:07
  • 2.1.5 :005 > Location.distance_from_sql('122.168.149.228') => "3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((27.433 - locations.latitude) * PI() / 180 / 2), 2) + COS(27.433 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((77.1 - locations.longitude) * PI() / 180 / 2), 2)))" 2.1.5 :006 > – Adt Oct 21 '15 at 17:12
  • @yez output as above – Adt Oct 21 '15 at 17:13
  • That looks fine, the next thing I would look at is that in your `dashboard_controller`, that the value of `remote_ip` is not nil. The big issue is that your scope seems to return `nil` for the `distance_from_sql` method during some process. – yez Oct 21 '15 at 17:16
  • @yez it may be the problem becz its sometime showing error and some time not – Adt Oct 21 '15 at 17:19
  • @yez thats the problem, any solution ? – Adt Oct 21 '15 at 17:36

1 Answers1

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.

yez
  • 2,368
  • 9
  • 15
  • problem still persist. problem is in scope :with_distance_to, ->(point) { select("#{table_name}.*").select("(#{distance_from_sql(point)}) as distance") } – Adt Oct 22 '15 at 07:58