I'm trying to handle geocoding in my Rails app in a way that I can restrict the flow of geocode api calls with DelayedJob.
My intention is just to queue geocoding requests, as they're not time sensitive to the user. Also I'm using a free geocoding API (Nominatim) that demands no more than a request per second.
I've got the geocoder gem setup, and in my User model I get the postcode from the user account setup (it can't be blank, and I'm already validating it).
My idea was to start throttling the calls using run_at, but initial tests of run_at show the job being queued, but the geocode not saving the the values on completion.
after_validation :run_geocode, :if => :postcode_changed?
def run_geocode
self.delay(:run_at => 30.seconds.from_now).geocode
end
Have I missed something pretty obvious here? I can't workout just what the :geocode method does that the docs say to use.
The geocoded response might not be saved if you don't call save anywhere. Also it's customary to run callbacks after saving / updating / deleting a record instead of after validation.
after_save :run_geocode, :if => :postcode_changed?
def run_geocode
self.delay.geocode! # delayed_job will process geocode! at a later point in time
end
def geocode!
# do whatever is nescessary to receive geocoded infos
# assign the results
# save the record to save the updates
self.save
end