Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2rails-geocoder

Implement the gecoder gem in nested resource loop?


I implemented the geocoder gem based on the tutorial on http://railscasts.com/episodes/273-geocoder

This is a part of the house show view template

- @house.location.nearbys(20, {:order => :distance, :units => :km}).each do |house|
   %li
    = link_to house.name, house_path
    (#{city.distance.round(2)} KM)

This show al other houses with the distance in KM, so this works fine.

I already showed on the same house page all the cities of the region where the house is located, like this:

%ul.tags
 - @region.cities.each do |city|
   %li= link_to city.name, country_region_city_path(@country, @region, city)

Question: How can i implement the geocoder gem in the @region.cities loop so the visitor can see on the house page how many KM is it to drive from the house to the cities?

Thanks...remco


Solution

  • Geocoded objects should have a distance_from method. @house should have that method if you follow Ryan Bates' tutorial.

    Check the docs for how to use it: https://github.com/alexreisner/geocoder#location-aware-database-queries

    You basically just need to ask @house it's distance from the city (within your cities loop)