Search code examples
ruby-on-railsruby-on-rails-3gmaps4rails

Added Gmaps4Rails to existing site - need to populate the lat long fields from existing address data


I am integrating gmaps4rails into an existing site and have it working well on the production site for records that have been created or updated since I rolled this out, however if a user pulls up a record that was created in the past the map will be blank because the lat and long fields have not been updated. I am looking for a way to run all records back through the update process so they will have the auto generated and maps will display correctly.

Model

acts_as_gmappable

def gmaps4rails_address
  "#{self.street}, #{self.city}, #{self.state}"
end

View

<%= gmaps({"map_options" => {"auto_adjust" => true, "auto_zoom" => false, "zoom" => 14},
                  "markers"=> {"data" => @json }}) %>

Is there something that i can run from the console that won't change any of the existing record data but will update the needed fields? Would the following get me what I want?

Workorder.all.update_attributes

Solution

  • Geocoding is triggered in a before_filter: process_geocoding defined here.

    For performance sake, I'd filter entries and load them in batches, I'd say:

    Workorder.where("latitude IS NULL").find_each do |order|
      order.save
    end
    

    A raw approach would be:

    Workorder.all.each do |order|
      order.save if order.latitude.blank?
    end