Rails :活动记录地址数组中的唯一地址

Rails : Unique address from array of active record addresses

如何检索具有相同纬度和经度的唯一地址

> addresses
=> [#<Address id: 10, country: "IN", state: "KA", latitude: "12.971513803339702", longitude: "77.59443104418949", user_id: 11>, #<Address id: 9, country: "IN", state: "KA", latitude: "12.971513803339702", longitude: "77.59443104418949", user_id: 10>]

在这种情况下,我需要任何一个 ActiveRecord,因为纬度和经度具有相似的值。

您可以按经纬度对地址进行分组,然后 select 至少包含 2 个地址的组:

addresses.
  group_by { |address| [address.latitude, address.longitude] }.
  select { |location, addresses| addresses.size >= 2 }.
  keys

根据您的用例,您可能想要使用 distinct(请注意,您会丢失 iduser_id 信息):

Address.select(:country, :state, :latitude, :longitude).distinct