ruby on rails -在子关联上使用地理编码器,如何找到给定位置的所有父关联


class User < AR
  has_many :locations
end
class Location < AR
  belongs_to :user
  geocoded_by :address
end

我如何写一个查询做到这一点?

@users = User._a_query_that_returns_users_in_'Paris'_

可能有一种更优雅的方法,但是您可以与Location表连接并使用地理编码器near方法来执行查询。像这样:

near = Location.near('Paris, France')
@users = User.joins(:locations).merge(near)

最新更新