Search code examples
ruby-on-railsruby-on-rails-3geolocationpolymorphic-associations

ruby geocoder gem with a polymorphic resource


I've a bunch or addresses which belong to various other resources via polymorphic association

belongs_to :addressable, :polymorphic => true

and my resource, lets say its a Hotel

has_one :address, :as => :addressable

I can search my address data no problem, find every address in a 20 mile radius of my search string for example

@addresses = Address.near(params[:search],20, :order => :distance).page params[:page]

I'm really struggling to join the resource i.e. the hotel info, I can link through to a details page, but I'd like to show the name of the hotel in my results for example.

I thought using :include may have helped thus:

@addresses = Address.near(params[:search],20, :order => :distance, :include => :hotel).page params[:page]

.. but no luck

Any advice greatly appreciated.

Stew


Solution

  • OK I figured it out with a little help from a colleague

     @addresses = Address.where(addressable_type: 'Hotel').near(params[:search],20, :order => :distance).includes(:addressable).page params[:page]
    

    Hope it might help someone else!