0
# search for a particular date (between two other dates)    
  a = self.where('created_at > ? AND created_at < ?', yes, tom)
# search for a particular location
  a = a.each do |loc|
    if coorDist(loc.lat, loc.lng, lat, lng) < dist
      return loc
    end
  end

我试图返回一个像a这里的初始值一样的哈希。如何遍历哈希并收集满足此if语句条件的实体?它在那里的方式,它只返回第一个结果。

4

2 回答 2

1
a.select! do |loc|
  coorDist(loc.lat, loc.lng, lat, long) < dist
end

如果您只是返回哈希,请使用select而不是select!.

于 2013-03-24T04:09:50.617 回答
1

代替#eachand if,使用#select.

# search for a particular location
a = a.select do |loc|
  coorDist(loc.lat, loc.lng, lat, lng) < dist
end
于 2013-03-24T04:18:41.453 回答