0

I am using the geocoder gem and I need to code a start and end address for a model, however I am not sure if I can accomplish this using this gem and if I can whether I am doing it correctly. If this cannot be done with the gem, is there another resource that will allow me to geocode two locations on the same form? This is an example of what I am trying to do, but it just passes the second address to be geocoded.

geocoded_by :start_address
before_validation :geocode
geocoded_by :delivery_address, :latitude => :end_latitude, :longitude => :end_longitude
before_validation :geocode
tomciopp
  • 2,602
  • 2
  • 31
  • 60

2 Answers2

1

If you look at the source it looks as though there's an options hash that would get overwritten active_record.rb and base.rb.

I figure there are two options: move your addresses into an included (joined) model (like Address or something), or fork geocoder to have multiple options by key. The first one is easier, and solves the problem. The second one is cooler (might play with that myself as a kata).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Is there any way that you could iterate through an array of addresses and then map their key to a new options hash? – tomciopp Sep 22 '11 at 21:53
1

So what's going on is just simple ruby... if you do this:

class Question
  def ask
    "what would you like?"
  end

  def ask
    "oh hai"
  end
end

Question.new.ask
 => "oh hai"

The last method definition wins... so when you declare two geocoded_by methods, the 2nd is the one that counts.

I think you're going to have to manually geocode, using the gem

before_validation :custom_geocoding

def custom_geocoding
  start_result = Geocoder.search(start_address)
  end_result = Geocoder.search(delivery_address)
  # manually assign lat/lng for each result
end
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • Could you elaborate on manually assigning the latitude and longitude, what I have tried has not worked. – tomciopp Sep 22 '11 at 18:22
  • self.start_latitude = start_result.latitude – Jesse Wolgamott Sep 22 '11 at 19:24
  • I just get a no method error in PostsController#create. However, I don't think I should be getting one because I am creating a post this way: def create @post = current_user.posts.build(params[:post]) end – tomciopp Sep 22 '11 at 19:53
  • if you can run this outside of your controller without error, then you have a different problem. if you get an error, post here – Jesse Wolgamott Sep 22 '11 at 21:01
  • not sure you got what I meant... without using a controller, so instead use the console, create your model – Jesse Wolgamott Sep 22 '11 at 23:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3714/discussion-between-jesse-wolgamott-and-demondeac11) – Jesse Wolgamott Sep 22 '11 at 23:16
  • I am still having issues and can't get geocoder to work with multiple addresses. I have tried every combination of code I can think of but still get errors when submitting. – tomciopp Sep 30 '11 at 05:41
  • In our talk, we discussed confirming you could save without your controller. We can either continue the discussion there, or you can move on and try a different approach. – Jesse Wolgamott Sep 30 '11 at 16:17
  • Here is a copy of all the code that I have written and what the error message shows when I have tried to create a post. [Github gist](https://gist.github.com/112cc28b7d52402079ad) – tomciopp Sep 30 '11 at 21:55
  • You don't have a current user. That's what this message is telling you `undefined method `loads' for nil:NilClass` – Jesse Wolgamott Sep 30 '11 at 23:14
  • If you'd like some more help with code or training -- hit me up at http://comalproductions.com or twitter.com/jwo – Jesse Wolgamott Sep 30 '11 at 23:15
  • I am using devise so I should have a current_user method. I can create a post when I am not using the custom geocoding method defined, so that shouldn't be the problem. – tomciopp Sep 30 '11 at 23:53
  • I am _extremely_ confident you do not. – Jesse Wolgamott Oct 01 '11 at 00:22
  • Ah, i was missing a "before_filter authenticate_user!" which should solve that issue, however I now get a different error with the Action Controller which i have updated in the github gist code. – tomciopp Oct 01 '11 at 01:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3953/discussion-between-demondeac11-and-jesse-wolgamott) – tomciopp Oct 01 '11 at 21:13
  • If I add this: current_user = User.find(params[:id]) to my create controller I get: Couldn't find User without an ID as the new error – tomciopp Oct 02 '11 at 00:18