Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7438803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:36:38+00:00 2026-05-29T10:36:38+00:00

I have imported a .csv file of 10,000 locations and I need to loop

  • 0

I have imported a .csv file of 10,000 locations and I need to loop through the database and geocode_by a few fields rather than the usual “geocode_by :address”

I am using the geocoder gem.

My database scheme looks like this

    create_table "locations", :force => true do |t|
      t.string   "Address"
      t.string   "City"
      t.string   "State"
      t.string   "Zip"
      t.float    "latitude"
      t.float    "longitude"
      t.datetime "created_at"
      t.datetime "updated_at"
    end 

Can I do this in a controller action rather than on validation?
Should I do something like this:

 def index
    @locations = Location.all
    @locations.each do |l|
        new_address = "#{l.Address} #{l.City} #{l.State}"
        geocode_by = :new_address
    end
 end

But yea, any help is greatly appreciated, thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:36:39+00:00Added an answer on May 29, 2026 at 10:36 am

    I was able to get things working with this controller code:

      def index
        if params[:search].present?
          @locations = Location.near(params[:search], 20, :order => :distance)
        else
          @locations = Location.all
          @locations.each do |l|
            if l.latitude.nil?
              new_location = "#{l.HP_Address_1} #{l.HP_City} #{l.HP_State}"
              s = Geocoder.search(new_location)
              l.latitude = s[0].latitude
              l.longitude = s[0].longitude
              l.save
            end
          end
        end
      end
    

    It loops through every entry in the database, and if the latitude and longitude values have not been encoded it calls the correct geocoder function, and stores the returned lat / long values in the database.

    I have 9k entries in my database, so I can only encode 2k per day due to the limit on the google maps api. But for folks used to encoding entries during creating using geocoded_by in their models, this will work in controllers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file to be imported to MYSQL database. The problem is
I have a table with 600+ columns imported from a CSV file with special
I have a very large CSV file that I imported into a sqlite table.
I have the following code for uploading a csv file to a mysql database.
I have imported a csv file containing spatial area information in varchar, then converted
I have a MySQL table that I have imported from a CSV file. In
I have imported a csv file into a list with this stmt: data1 =
I have my CSV file imported as such: records = FasterCSV.read(path, :headers => true,
I have been handed a very large CSV file that needs to be imported
I have a scenario where data needs to be imported from a CSV file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.