0

using the geocoder gem

Can someone tell me can i get the City and State to automatically be entered into the database if i add those columns.

in the example below i am passing in :cs witch is an address

class Location < ActiveRecord::Base
  attr_accessible :cs, :latitude, :longitude
  geocoded_by :cs
  after_validation :geocode, :if => :cs_changed?
  has_many :shipments, :foreign_key => :origin_id
  has_many :shipments, :foreign_key => :dest_id
end

I need to save the geocoded city and state that was parsed.

either that or figure out how to get the gecoder to give me a list of locations in a particular group of states from the database

I would be passing in array [mo,il,ks,ar] or string "mo,il,ks,ar"

i have tried:

Location.near([mo,il,ks,ar])

I can however do:

Location.near("springfield, mo", 20)
Big Al Ruby Newbie
  • 834
  • 1
  • 10
  • 30

1 Answers1

0

I figured it out after alot of research i added the columns City and State and did this:

Actual Working Code: this will add the City and State from the Results...

class Location < ActiveRecord::Base
  attr_accessible :cs, :latitude, :longitude, :city, :state

  after_validation :geocode, :if => :cs_changed?
  has_many :shipments, :foreign_key => :origin_id
  has_many :shipments, :foreign_key => :dest_id

  geocoded_by :cs do |obj, results|
    geo = results.first
    if geo
      obj.latitude = geo.latitude
      obj.longitude = geo.longitude
      obj.city  = geo.city
      obj.state = geo.state_code
    end
  end
end

Would also like to say that my input field is utilizing the GeoComplete Gem that gives valid City, St, Country combinations before it gets here so should be able to find the right location

Big Al Ruby Newbie
  • 834
  • 1
  • 10
  • 30