I have a form that the user uses to enter a physical address, and I want to use the GeoCoder gem to convert that physical address to a lat/long value set that I can also store in the database when the model they form submission creates gets saved.
I'm having trouble deciding where I should put this call to use GeoCoder to populate the latitude and longitude values for the database. I thought it would go in the model class, but was not sure if I should do it in an initialize method where I accept the physical address and then inside the initialize method I use call to geocoder to set values for @latitude and @longitude, or is there a better way or place to do this such as simply using attr_accessor in the model and then calling it to set these values in the controller before the save action completes.
I guess my main confusion is due to in the controller in the new and create methods, there is not much there so could not figure out how to get this done upstream of the "save" that inserts the data for the resource into the database.
Fat models, skinny controllers, and a whole bunch of service objects.
But for the sake of "just getting it done", model works fine.
The idea being is that you should be able to call the same method (in this case, your Geocoding method) anywhere without having to copy-pasta code. You know, that whole don't-repeat-yourself thing.
In your (example) place.rb
before_save :if => { |_| self.address_changed? && self.address.present? } do
result = Geocoder.search(self.address)
self.lat, self.lng = result.lat, result.lng
end