Date: 2024-03-19
Accepted
We want to add longitude and latitude attributes to our School
records, so we can search for schools within a given area (e.g 10 miles from London, United Kingdom).
We have decided to use the Geocoder gem to geocode our School
records. Geocoding is based of the address attributes assigned to a school.
School Address Attributes
- Address1
- Address2
- Address3
- Town
- Postcode
Using the method geocode
on a School
record will dispatch an API call to an external provider, and retrieve the longitude and latitude of the school based on the schools's School Address Attributes. The retrieved longitude and latitude values are then assigned to the respective attributes in the School
record.
(We have hardcoded the address method in the School
model to only reference addresses within the United Kingdom)
-
Due to our decision to use the
upsert_all
method in theGias::CSVImporter
service, the validation callback in theSchool
model will not trigger thegeocode
method when creating or updating a school using theGias::CSVImporter
service. As a result we have implemented theGeocoder::UpdateAllSchools
service andGeocoder::UpdateAllSchoolsJob
job to run as part of theGias::SyncAllSchoolsJob
job, to geocode all un-geocoded schools. -
If any of a school's School Address Attributes were to be updated as a result of the
Gias::SyncAllSchoolsJob
job, the school record will not be re-geocoded, as the school record has already been geocoded. To re-geocode the school record, you will need to run thegeocode
method on the updated school manually from the Rails console. (However, we find it unlikely that a school will change its address drastically enough to warrant re-geocoding upon a change to the School Address Attributes)