Search code examples
ruby-on-rails-4rails-activerecordgeokit

geokit-rails getting the route passing close to 2 points


I'm having a route model

class Route < ActiveRecord::Base
  has_many :etapes
  acts_as_mappable :through => :steps
end

and a step one (that contains lat and lgn)

class Step ActiveRecord::Base
  belongs_to :route
  acts_as_mappable
end

Actually, I can get the routes passing close to one point with the in_range scope : Route.joins(:steps).in_range(0..15, :origin => [lat, lng]).group(:id)

I'm trying to get the route passing close to 2 steps, acts_as_mappable does not have the scope I need, so I'm wondering what the best way to go ?


Solution

  • Well, to those having similar issue, I did it like this.

    Given a hash with 4 points (2 lat and lng);

    ids = Route.joins(:steps).in_range(0..15, :origin => [hash[:lat1], hash[:lng1]]).group(:id).map(&:id)
    routes = Route.joins(:steps).in_range(0..15, :origin => [hash[:lat2], hash[:lng2]]).group(:id)
    routes.select {|route| ids.include? route.id}
    

    I first take the routes in range with my first point, then the routes in range with my second point and I select in my first routes only the one in the second routes.