geokit / geokit-rails

Official Geokit plugin for Rails/ActiveRecord. Provides location-based goodness for your Rails app. Requires the Geokit gem.
MIT License
1.57k stars 245 forks source link

Recommended way to work with model with multiple geocoded associations #35

Open keithnorm opened 11 years ago

keithnorm commented 11 years ago

Just curious how you guys would handle a scenario like this:

Say you have a Trip model and a Trip has an origin_location and a destination_location. Hypothetically something like:

class Trip < ActiveRecord::Base 
  belongs_to :origin_location, :class_name => 'Location'
  belongs_to :destination_location, :class_name => 'Location'
  acts_as_mappable :through => :origin_location
  acts_as_mappable :through => :destination_location
end

class Location < ActiveRecord::Base
  acts_as_mappable
end

And I'm kind of thinking I want to make calls like:

Trip.destination_location_within(50, [lat, lng]) and Trip.origin_location_within(50, [lat, lng])

Do you think that makes sense, or should this instead be modeled differently? Any insight / advice on how to handle this situation?

Thanks!

DaveSanders commented 11 years ago

I'm looking for the same advice. I have a very similar model and am looking at the various gems, and am not finding any good answers. Especially when I want to chain those queries together and find an origin within one radius and the destination within another. Did you get any where with this?

keithnorm commented 11 years ago

I punted on it for now and am working around it in my project.

I think it's worth a fork / pull request to add it into geokit-rails and I may work on that still, but it would be cool to get a "yeah that's a good idea" from the geokit-rails team before doing that work.

Without doing any work to geokit-rails, I think something like this would work technically

origin_location_ids = Location.select(:id).within(50, [origin_lat, origin_lng])
destination_location_ids = Location.select(:id).within(50, [destination_lat, destination_lng])
trips = Trip.where(:destination_location => destination_location_ids, :origin_location => origin_location_ids)

but I think that's ugly and not sure if it has scaling or other problems.