Skip to content
Snippets Groups Projects
Commit a0eecf98 authored by Steve Lawson's avatar Steve Lawson
Browse files

Use ActiveRecord .primary_key instead of :id when building exclusion condition

Fixes: #194 - add_exclude_condition breaks on tables without ID field - https://github.com/alexreisner/geocoder/issues/194
parent 12444f7f
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
# 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
......@@ -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.
#
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment