← Back to Upcase

Geolocation with multiple models


(Matt Bjornson) #1

I’m working on an app that requires (at least) two ActiveRecord models to contain geolocation information. I’m using the Geocoder gem. One of the models is User and the other is Poll. I’ve currently implemented this using a Polymorphic association like so,

class User < ActiveRecord::Base
  has_many :geolocations, as: :geolocationable
  ...

end

class Poll < ActiveRecord::Base
  has_one :geolocation, as: :geolocationable
  ...
end

class Geolocation < ActiveRecord::Base
  belongs_to :geolocationable, polymorphic: true
  ...
end

Or would this be cleaner to use ActiveSupport::Concerns for an approach?

I’ve also run into an issue in that I’d love to be able to use Geocoders class method near to find the nearest poll for example. Using the Polymorphic association, I’m struggling to find a solution to this problem. ( Note: I think this is a simple implementation using the ActiveSupport:Concerns approach) Thoughts?

Thanks! Matt