9
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'_
Gavin
  • 4,273
  • 3
  • 27
  • 39

1 Answers1

16

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)
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • In Rails 7, the merge removes all the parent table attributes: `Profile.joins(:address).merge(Address.near('Dallas, TX')).first.attributes.keys ` `Profile Load (3.8ms) => ["id", "location", "latitude", "longitude", "city", "state", "state_code", "postal_code", "country", "country_code", "created_at", "updated_at", "distance", "bearing"]` – MDWPILOT Dec 10 '22 at 14:04