Skip to content

Commit a8a7350

Browse files
committed
Fix AR type cast to mirror quoting
1 parent 9bb0135 commit a8a7350

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/active_record/connection_adapters/postgis/quoting.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ module ConnectionAdapters
55
module PostGIS
66
module Quoting
77
def type_cast(value)
8-
case value
9-
when RGeo::Feature::Instance
8+
if RGeo::Feature::Geometry.check_type(value)
9+
RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)
10+
elsif value.is_a?(RGeo::Cartesian::BoundingBox)
1011
value.to_s
1112
else
1213
super

test/cases/spatial_queries_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ def test_geo_safe_where
122122
assert_equal 1, SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", geographic_factory.point(-72.099, 42.099)).count
123123
end
124124

125+
def test_geo_query_matches_sql
126+
value = geographic_factory.point(-72.099, 42.099)
127+
assert_match(
128+
RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value),
129+
SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", value).explain.inspect
130+
)
131+
end
132+
125133
private
126134

127135
def create_model

0 commit comments

Comments
 (0)