Hassen
Hassen

Reputation: 7634

Skip geocoding in some cases

I'm using the Geocode gem in order to convert addresses to (logitude, latitude) coordinates. In order to do so, I added this in the user Profile model that is geocoded:

geocoded_by :address

after_validation :geocode, if: Proc.new { |p| p.address_changed? }

This works great, but now, I'm making a migration script that populate the Profiles table with addresses, and also longitudes and latitudes. In this case, geocoder is still doing the conversion, and I get the following warning many times:

Google Geocoding API error: over query limit.

How can I skip the geocoding in some cases, like in migration when I already have coordinates?

Upvotes: 1

Views: 571

Answers (2)

Meekohi
Meekohi

Reputation: 10882

The right answer depends on how you implemented address_changed?.

The recommended is:

after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }

but I tend to use:

after_validation :geocode, if: ->(obj){ obj.address.present? and !obj.address.latitude? and !obj.address.longitude? }

in which case you will skip the geocoding if a latitude,longitude pair has already been set.

Upvotes: 1

apradillap
apradillap

Reputation: 83

In config/initializers/geocoder.rb put the following code:

Geocoder::Configuration.lookup = :yandex

for update to quota: 25000 requests / day

More details here.

Upvotes: 0

Related Questions