简体   繁体   中英

Address error with the Rails GeoCoder gem

I am using the GeoCoder gem to lookup lat and longs for locations, and its seems to working fine in the rails code. However to get each locations lat/long is going to be long winded and take quite a while.

Looking through the docs I have seen I can use rake geocode:all CLASS=Location SLEEP=0.25 BATCH=100 to process batches of 100.

Using this I am running into the following error...

NoMethodError: undefined method `address=' for #<Location:0x00007f9cf3d8daf0>
Did you mean?  address1=
               address2=
               address3=
               address4=
               address
               address4
               address2
               address1
               address3
               address3?
               address2?
               address1?
               address4?

Following the docs, I have set up an address method as shown in the Location model below.

class Location < ApplicationRecord
  geocoded_by :address
    reverse_geocoded_by :latitude, :longitude
    after_validation :geocode, :reverse_geocode

    # searchkick locations: [:location]

    def address
        [name, postcode].compact.join(", ")
    end

    #  def search_data
  #   attributes.merge location: { lat: latitude, lon: longitude }
  # end
end

I have looked through StackOverflow, and spend quite some time on Google trying to work out what is causing this issue, but can't for the life of me get it to work.

Any one have any pointers, or can see where I am going wrong?

Thanks

Geocoder expects there to be an:address column in your model with the reverse geocoding. You can change the default like so:

reverse_geocoded_by :latitude, :longitude, :address
after_validation :reverse_geocode

I would run a migration to add an address column to your table. So the migration can be like:

rails g model model_name address latitude:float longitude:float

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM