【发布时间】:2020-07-21 00:48:18
【问题描述】:
area 有很多coordinates
coordinate 模型有两个属性:latitude 和 longitude
还有什么可以重构的吗?现在它可以工作,但它会执行一种 n+1 查询:
areas.each_with_object([]) do |area, array|
geokit_lat_lngs = area.coordinates.order(sequence: :asc).map do |coordinate|
Geokit::LatLng.new(coordinate.latitude, coordinate.longitude)
end
polygon = Geokit::Polygon.new(geokit_lat_lngs)
point = Geokit::LatLng.new(latitude, longitude)
array << area.id if polygon.contains?(point)
end
我正在尝试获取所有 latitude 和 longitude 属于的 areas
我正在使用 Geokit gem,但如果我应该做一些更有效的事情,我很乐意切换
【问题讨论】:
-
你在用postgresql吗,那么你可能想直接在数据库级别做。点 postgresql.org/docs/9.3/datatype-geometric.html#AEN6706 有特定的数据类型,但不确定如何构建查询,但有可能,浏览一下,在:stackoverflow.com/questions/10034636/…
标签: ruby-on-rails ruby geokit