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

1

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'_
Pastel answered 7/1, 2013 at 1:8 Comment(0)
I
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)
Instar answered 7/1, 2013 at 2:10 Comment(1)
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"]Sensitometer

© 2022 - 2024 — McMap. All rights reserved.