Search code examples
ruby-on-railsgeokit

"undefined method `/' for "":String:" Error when implementing geokit


I'm trying to use geokit to find all the entities next to a given set of coordinates, I've been trying for quite a few hours now and can't seem to find what the issue is.

Initially thought it would have something to do with the Latitude and Longitude columns being String, so just ran a migration to convert them into Decimal following this guideline, yet no luck as I am still getting the following exception.

Here is my model:

class Video < ActiveRecord::Base
  include Rails.application.routes.url_helpers
  include ActiveModel::Dirty

  belongs_to :user
  belongs_to :camera
  belongs_to :lens
  belongs_to :airframe
  belongs_to :codec
  belongs_to :video_format
  belongs_to :updated_by, :class_name => "User"

  attr_accessible :duration, :location, :user_id, :camera_id, :lens_id, :video_format_id, 
                  :codec_id, :title, :description, :source_filename, 
                  :latitude, :longitude, :airframe_id, :resolution, :framerate, :bitrate,
                  :recording_datetime, :permissions, :source_video, :status, :terms_and_conditions, 
                  :updated_by_id, :video_tags_attributes, :staff_pick, :gallery_id

  acts_as_mappable :lat_column_name => :latitude,
                   :lng_column_name => :longitude

end

And my controller's action:

def index_by_location
    origin = Geokit::LatLng.new(32.91663,-96.982841)
    #origin = Video.geo_scope(:origin => [37.792,-122.393]) does not work
    videos = Video.in_range(2..5, :origin => origin)
    render :json => videos
  end

And this is the error I'm getting:

 NoMethodError: undefined method `/' for "":String: SELECT "videos".* FROM "videos"  WHERE ((
          (CASE WHEN videos.latitude IS NULL OR videos.longitude IS NULL THEN NULL ELSE
          (ACOS(least(1,COS(0.5745035721607411)*COS(-1.6926698933881499)*COS(RADIANS(videos.latitude))*COS(RADIANS(videos.longitude))+
          COS(0.5745035721607411)*SIN(-1.6926698933881499)*COS(RADIANS(videos.latitude))*SIN(RADIANS(videos.longitude))+
          SIN(0.5745035721607411)*SIN(RADIANS(videos.latitude))))*3963.1899999999996)
          END)
          >= 2 AND 
          (CASE WHEN videos.latitude IS NULL OR videos.longitude IS NULL THEN NULL ELSE
          (ACOS(least(1,COS(0.5745035721607411)*COS(-1.6926698933881499)*COS(RADIANS(videos.latitude))*COS(RADIANS(videos.longitude))+
          COS(0.5745035721607411)*SIN(-1.6926698933881499)*COS(RADIANS(videos.latitude))*SIN(RADIANS(videos.longitude))+
          SIN(0.5745035721607411)*SIN(RADIANS(videos.latitude))))*3963.1899999999996)
          END)
          <= 5))

Could anyone please help? Thanks!


Solution

  • Okay, great!

    So, doing some testing, it just seems that for some reason geokit doesn't like SQLite. Also, I rolled back the change to decimal and it works as well, which means it does not matter wether if your latitude and longitude fields are String.