geokit / geokit-rails

Official Geokit plugin for Rails/ActiveRecord. Provides location-based goodness for your Rails app. Requires the Geokit gem.
MIT License
1.57k stars 245 forks source link

Nested lat/lng column names #147

Closed NomNomCameron closed 3 years ago

NomNomCameron commented 5 years ago

Instead of the lat and lng columns having to be directly accessible on the table using chain notation (e.g. Location.lat), is there any current support (or would it be useful to someone other than me) to have the ability to specify accessing the lat lng data from a nested attribute (json for example)?

Something like House.location["lat"] is what I'm looking for

ryankopf commented 3 years ago

This is almost certainly not going to be supported or possible.

Keep in mind to do what your asking would required deserializing every single JSON object in your database every time you try to perform a query. Imagine if you have 100,000 records in the database, every time you try to find something near a certain latitude and longitude you have to deserialize the JSON data to get the latitude and longitudes to compare to, which would be impractically slow. The slowness will increase by several orders of magnitude the more records you have. If you have just the limited data set, you could write a function to deserialize these yourself and make a comparison, but your best bet is to make these direct attributes of your model/table columns.

NomNomCameron commented 3 years ago

Postgres supports jsonb, along with indexing these columns. What deserialization would have to take place in order to lookup lat and lng from a jsonb column type?

ryankopf commented 3 years ago

I didn't know that about postgres. Can you tell me, is it's use supported elsewhere by rails? IE, can you do this:

Person.where("person.location.lat = ?", latitude)

ryankopf commented 3 years ago

Or, more railsy, Person.where(location_latitude: latitude)