diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb index 96b11e0701a2c3a3873e0930b295e1822303fc14..8250623c25f3783faaad0231617b7967313c910b 100644 --- a/lib/geocoder/stores/active_record.rb +++ b/lib/geocoder/stores/active_record.rb @@ -253,7 +253,7 @@ module Geocoder::Store # def add_exclude_condition(conditions, exclude) if exclude - conditions[0] << " AND #{full_column_name(:id)} != ?" + conditions[0] << " AND #{full_column_name(primary_key)} != ?" conditions << exclude.id end conditions diff --git a/test/active_record_test.rb b/test/active_record_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..6d2d2316b085a5584935385d0379e5a7c1072fc3 --- /dev/null +++ b/test/active_record_test.rb @@ -0,0 +1,15 @@ +# encoding: utf-8 +require 'test_helper' + +class ActiveRecordTest < Test::Unit::TestCase + + def test_exclude_condition_when_model_has_a_custom_primary_key + venue = VenuePlus.new(*venue_params(:msg)) + + # just call private method directly so we don't have to stub .near scope + conditions = venue.class.send(:add_exclude_condition, ["fake_condition"], venue) + + assert_match( /#{VenuePlus.primary_key}/, conditions.join) + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8ac6b3e0a6a939e7635eca5e6f63479a532c4a4c..e832956256d7bc4a2b0f559360f1ea484c03f44c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -35,6 +35,17 @@ module ActiveRecord read_attribute name end end + + class << self + def table_name + 'test_table_name' + end + + def primary_key + :id + end + end + end end @@ -192,6 +203,20 @@ class Venue < ActiveRecord::Base end end +## +# Geocoded model. +# - Has user-defined primary key (not just 'id') +# +class VenuePlus < Venue + + class << self + def primary_key + :custom_primary_key_id + end + end + +end + ## # Reverse geocoded model. #