ruby-on-railsactiverecordgeocodinggeocoder

Rails - Can I use Geocoder to locate records where only the user has been geocoded?


A slightly frustrating issue with Geocoder.

I have two models:

User => has_many :records

Record => belongs_to :user

I'm trying to write the logic for a search function that will return all the record instances that are geographically close to the current_user. This is along with other query params.

I understand that I can use User.near to get nearby users, but I can't find a straightforward way to extract the records that belong to those users and just return those.

Ideally, I'd like something along the lines of:

records = Record.where(User.near(user, 5))

However, I know that this won't work.

Any suggestions would be most welcome! Thanks


Solution

  • You could try geocoder's nearbys method to get all User in 5 miles range, then get all records, something like:

    records = current_user.nearbys(5).collect(&:record)