geokit / geokit-rails

Official Geokit plugin for Rails/ActiveRecord. Provides location-based goodness for your Rails app. Requires the Geokit gem.
MIT License
1.57k stars 245 forks source link

TypeError using by_distance on sqlite3 #155

Closed kmmndr closed 3 years ago

kmmndr commented 4 years ago

Calling by_distance scope on sqlite3 produce the following error

TypeError (no implicit conversion of Symbol into Integer):

sqlite3 (1.4.2) lib/sqlite3/database.rb:396:in `define_function_with_flags'
sqlite3 (1.4.2) lib/sqlite3/database.rb:396:in `create_function'
geokit-rails (2.3.1) lib/geokit-rails/adapters/sqlite.rb:6:in `add_numeric'
geokit-rails (2.3.1) lib/geokit-rails/adapters/sqlite.rb:13:in `add_math'
geokit-rails (2.3.1) lib/geokit-rails/adapters/sqlite.rb:22:in `load'
geokit-rails (2.3.1) lib/geokit-rails/acts_as_mappable.rb:111:in `geokit_finder_adapter'
geokit-rails (2.3.1) lib/geokit-rails/acts_as_mappable.rb:345:in `sphere_distance_sql'
geokit-rails (2.3.1) lib/geokit-rails/acts_as_mappable.rb:215:in `distance_sql'
geokit-rails (2.3.1) lib/geokit-rails/acts_as_mappable.rb:155:in `by_distance' 
kmmndr commented 4 years ago

It seems to be between geokit-rails and sqlite3-ruby as the former is sending a symbol as third argument and the later expecting the third argument to be found as integer in an enum

# lib/geokit-rails/adapters/sqlite.rb
      def self.add_numeric(name) 
        @@connection.create_function name, 1, :numeric do |func, *args|
          func.result = yield(*args)
        end
      end
# lib/sqlite3/database.rb
    def create_function name, arity, text_rep=Constants::TextRep::UTF8, &block
      define_function_with_flags(name, text_rep) do |*args|
        fp = FunctionProxy.new
        block.call(fp, *args)
        fp.result
      end
      self
    end
kmmndr commented 4 years ago

Removing the third argument (and keeping UTF8 by default) works as expected

diff --git a/lib/geokit-rails/adapters/sqlite.rb b/lib/geokit-rails/adapters/sqlite.rb
index 1042eff..ae1754c 100644
--- a/lib/geokit-rails/adapters/sqlite.rb
+++ b/lib/geokit-rails/adapters/sqlite.rb
@@ -3,7 +3,7 @@ module Geokit
     class SQLite < Abstract

       def self.add_numeric(name) 
-        @@connection.create_function name, 1, :numeric do |func, *args|
+        @@connection.create_function name, 1 do |func, *args|
           func.result = yield(*args)
         end
       end
paulogeyer commented 4 years ago

+1

I have the same problem here, this patch is going to be added to the master?

ryankopf commented 3 years ago

This is fixed in 2.3.2 now.