diff --git a/Gemfile b/Gemfile index b114997e63766aaffb45ad5066df769b59516390..85c71ce03310e9930678f9c4b5bd51b719aa3a9e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,8 @@ source "http://rubygems.org" -gemspec - group :development, :test do gem 'rake' - gem 'mongoid' + gem 'mongoid', '3.0.13' gem 'bson_ext', :platforms => :ruby gem 'rails' @@ -13,3 +11,5 @@ group :development, :test do gem 'jruby-openssl' end end + +gemspec diff --git a/README.md b/README.md index a66578656bf392e5d655d3c3a2fee6513e3fef62..91cccb9833bcbd867de55246cf16987d07c79b99 100644 --- a/README.md +++ b/README.md @@ -590,6 +590,26 @@ You can also do this to raise all exceptions: See `lib/geocoder/exceptions.rb` for a list of raise-able exceptions. +Troubleshooting +--------------- + +### Mongoid + +If you get one of these errors: + + uninitialized constant Geocoder::Model::Mongoid + uninitialized constant Geocoder::Model::Mongoid::Mongo + +you should check your Gemfile to make sure the Mongoid gem is listed _before_ Geocoder. If Mongoid isn't loaded when Geocoder is initialized, Geocoder will not load support for Mongoid. + +### ActiveRecord + +A lot of debugging time can be saved by understanding how Geocoder works with ActiveRecord. When you use the `near` scope or the `nearbys` method of a geocoded object, Geocoder creates an ActiveModel::Relation object which adds some attributes (eg: distance, bearing) to the SELECT clause. It also adds a condition to the WHERE clause to check that distance is within the given radius. Because the SELECT clause is modified, anything else that modifies the SELECT clause may produce strange results, for example: + +* using the `pluck` method (selects only a single column) +* specifying another model through `includes` (selects columns from other tables) + + Known Issue ----------- diff --git a/lib/geocoder/sql.rb b/lib/geocoder/sql.rb index 2ee2795c50a8d38faf60dcda853aa267c9dcc417..af0b4983a84cd6275b56da4de70fbe8514282a8e 100644 --- a/lib/geocoder/sql.rb +++ b/lib/geocoder/sql.rb @@ -11,7 +11,8 @@ module Geocoder # http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL # def full_distance(latitude, longitude, lat_attr, lon_attr, options = {}) - earth = Geocoder::Calculations.earth_radius(options[:units] || :mi) + units = options[:units] || Geocoder::Configuration.units + earth = Geocoder::Calculations.earth_radius(units) "#{earth} * 2 * ASIN(SQRT(" + "POWER(SIN((#{latitude.to_f} - #{lat_attr}) * PI() / 180 / 2), 2) + " + @@ -31,8 +32,9 @@ module Geocoder # are not intended for use in production! # def approx_distance(latitude, longitude, lat_attr, lon_attr, options = {}) - dx = Geocoder::Calculations.longitude_degree_distance(30, options[:units] || :mi) - dy = Geocoder::Calculations.latitude_degree_distance(options[:units] || :mi) + units = options[:units] || Geocoder::Configuration.units + dx = Geocoder::Calculations.longitude_degree_distance(30, units) + dy = Geocoder::Calculations.latitude_degree_distance(units) # sin of 45 degrees = average x or y component of vector factor = Math.sin(Math::PI / 4) @@ -61,7 +63,7 @@ module Geocoder # http://www.beginningspatial.com/calculating_bearing_one_point_another # def full_bearing(latitude, longitude, lat_attr, lon_attr, options = {}) - case options[:bearing] + case options[:bearing] || Geocoder::Configuration.distances when :linear "CAST(" + "DEGREES(ATAN2( " +