rgeo / rgeo-activerecord

RGeo ActiveRecord extensions and tools for spatial connection adapters
Other
89 stars 64 forks source link

Fix alias issue #66

Closed ugisozols closed 3 years ago

ugisozols commented 3 years ago

I have this code:

point = RGeo::Geographic.spherical_factory(srid: 4326).point(1.0, 1.0)

MyModel.where("ST_DWithin(my_models.lonlat, ST_GeographyFromText('SRID=4326;POINT(1.0 1.0)'), 10)").select(Arel.star, MyModel.arel_table[:lonlat].st_distance("SRID=4326;#{point}").as('distance')).to_sql

Rails 6.0.3.5 rgeo-activerecord 6.2.1

=> "SELECT *, ST_Distance(\"my_models\".\"lonlat\", ST_GeomFromEWKT('SRID=4326;POINT (1.0 1.0)')) AS distance FROM \"my_models\" WHERE (ST_DWithin(my_models.lonlat, ST_GeographyFromText('SRID=4326;POINT(1.0 1.0)'), 10))"

Rails 6.1.2.1 rgeo-activerecord 7.0.0

=> "SELECT *, ST_Distance(\"my_models\".\"lonlat\", ST_GeomFromEWKT('SRID=4326;POINT (1.0 1.0)'))distance AS #<Arel::Collectors::SubstituteBinds:0x00007ff9fd8f1390> FROM \"my_models\" WHERE (ST_DWithin(my_models.lonlat, ST_GeographyFromText('SRID=4326;POINT(1.0 1.0)'), 10))"

Used the code from Rails repo which seems to generate the right query.

I have no idea how to write a test for it or even if it's the right fix 😄

keithdoggett commented 3 years ago

Thanks for looking into this and coming up with a solution! I'm going to try to write a few tests for alias and distinct since those are currently not tested. I'll post them here and you can copy them into the PR.

keithdoggett commented 3 years ago

I ended up changing the test suite around a little bit and it's easier to just do it in a separate PR. Can you change your target branch to https://github.com/rgeo/rgeo-activerecord/pull/67? Once you've done that I'll merge and we can get this into master.

ugisozols commented 3 years ago

@keithdoggett done.

keithdoggett commented 3 years ago

Thanks!