I need to use the Ruby Geocoder gem to create an object using attributes back from an address search query. I expect to create a new location from the received data from the Geocoder results and create a new object with attributes from the migration. I have searched resources online to see how to create an object from the results but I need to know how to get the locations attributes from longitude and latitude co-ordinates.
expected example search query: 'Wall St, NY'
=> {address: "Wall Street", city: "New York, state: "New York", country: "United States of America"
Location Model
class Location < ApplicationRecord
has_many :users
geocoded_by :address
after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
end
LocationController#create
def create
@location = Location.new(location_params)
if @location.save
flash[:success] = "location added!"
redirect_to location_path(@location)
else
render 'new'
end
end
Migration
class CreateLocations < ActiveRecord::Migration[6.0]
def change
create_table :locations do |t|
t.string :address
t.string :city
t.string :state
t.string :country
t.float :longitude
t.float :latitude
end
end
end