Rails Geocoder, Find all Model1 near Model2

72 views Asked by At

I have a 2 models that are geo coded. One of the model is Client.I update the location of the client model based on their IP. Here's how it looks

class Client < ApplicationRecord
  geocoded_by :ip_address
  after_validation :geocode, :if => :ip_address_changed?
end

Here's my client controller

class ClientController < ApplicationController
    def index
        if client_signed_in?
            u = current_client
            u.ip_address = request.remote_ip
            u.save
        end
    end
end

now as you can see I update the client's current ip and based on that I save the location of the client in :longitude and :latitude attributes

Now I have another model named Hotels. This Model has a address attribute that does not change.

class Hotel < ApplicationRecord
  geocoded_by :address
  after_validation :geocode, :if => :address_changed?
end

now I need a way so that I can find Hotels that are near 20 km radius of a client. I have tried @client.nearbys(20) but that will give me other clients nearby where I want hotels nearby.

Someone please help. Thanks in advance :)

1

There are 1 answers

2
kajal ojha On BEST ANSWER

As long as I can understand you have to find the hotel that are within 20km of a client.

So since you are able to find the latitude and longitude of a client you can Search a Hotel that is 20km away from that lat and long.

[email protected]
[email protected]

Hotel.near([lat,long], 20, :units => :km)