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

  • Home
  • SEARCH
  • 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 282617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:18:14+00:00 2026-05-12T05:18:14+00:00

My Address class has a geocode class method that returns an array of address

  • 0

My Address class has a geocode class method that returns an array of address objects derived from geocoding the method’s parameter (if the geocoding results in an exact match, the array will have one element).

One annoying part about writing this method is translating the GeoKit address objects to my address objects (e.g., “street_address” -> “address1”). Is there a better way to do this?

class Address < ActiveRecord::Base
  def self.geocode(string)
    return nil if string.nil?

    results = Geokit::Geocoders::GoogleGeocoder.geocode(string)

    address_objects = Array.new

    results.all.each do |r|
      params = Hash.new

      params['address1'] = r.street_address
      params['city'] = r.city
      params['zipcode'] = r.zip
      params['state'] = State.find_by_abbr(r.state)
      params['country'] = Country.find_by_iso(r.country_code)

      new_address = Address.new(params)
      new_address.single_line_address = r.full_address

      address_objects << new_address    
    end

    return address_objects
  end
end
  • 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-12T05:18:15+00:00Added an answer on May 12, 2026 at 5:18 am

    What about the following

    class Address < ActiveRecord::Base
      def self.geocode(string)
        return if string.nil?
    
        results = Geokit::Geocoders::GoogleGeocoder.geocode(string)
        results.all.map do |r|
          Address.new do |address|
            address.address1  = r.street_address
            address.city      = r.city
            address.zipcode   = r.zip
            address.state     = State.find_by_abbr(r.state)
            address.country   = Country.find_by_iso(r.country_code)
            address.single_line_address = r.full_address
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a singleton class which uses GetInstance() method to get the instance address
I have Address and Organization tables. Address has FK named organizationID . Address class
I have a User class that has a UserProfile class. The email is stored
The scenario: a User class has several groups of properties: password , address ,
Lets say that one has a class like Person that has associated with it
I have an address class that uses a regular expression to parse the house
I'm trying to make a basic Address class, that many different other classes can
I have made a map overlay class and have overridden the onTouchEvent method that
I've got a Mongoid Node model that has_many addresses (Address class). I'm using a
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <

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.