Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using geocoder on a child association, how to find all parents in a given location?

class User < AR
  has_many :locations
end

class Location < AR
  belongs_to :user
  geocoded_by :address
end

How do I write a query that does this?

@users = User._a_query_that_returns_users_in_'Paris'_
like image 756
Gavin Avatar asked Jan 07 '13 01:01

Gavin


1 Answers

There might be a more elegant approach, but you can join with the Location table and use the geocoder near method to do the query. Something like this:

near = Location.near('Paris, France')
@users = User.joins(:locations).merge(near)
like image 106
Peter Brown Avatar answered Nov 02 '22 04:11

Peter Brown