Artyom Gunkin
Artyom Gunkin

Reputation: 73

How to use two locations with Geocoder rails

I use geocoder gem in Rails, but my model has two locataions (route_from, route_to).
I can use it only for one attribute. But I won't use a both.

geocoded_by :address  
after_validation :geocode 


What do I do?

Upvotes: 0

Views: 109

Answers (1)

max
max

Reputation: 101811

Geocoder is not really built to have handle locations in the same model. Rather than beating a skrew in with a hammer you should split your domain into two models:

class Journey < ActiveRecord::Base
  has_one :route_from, class: 'Waypoint'
  has_one :route_to, class: 'Waypoint'
end

class Waypoint < ActiveRecord::Base
  reverse_geocoded_by :latitude, :longitude
  after_validation :reverse_geocode  # auto-fetch address
end

If you can imagine the route having more than two nodes you might want to set it up with a join model.

Upvotes: 1

Related Questions