0
votes

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?

1
What's your problem, exactly? What is causing you troubles? You should expand the question with more details. - tompave

1 Answers

1
votes

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.