给定一个具有三维坐标x,y和z的系统模型,我写了下面的实例方法给我所有其他系统在一个设置范围内具体系统的问题。这里的代码:将find_by_sql语法转换为Rails 4中正确的Active Record AREL语法
def systems_within(range = '15')
System.find_by_sql(["SELECT * FROM systems WHERE Sqrt(Pow((:x - systems.x), 2) + Pow((:y - systems.y), 2) + Pow((:z - systems.z), 2)) <= :range AND id!= :current_id", {x: x, y: y, z: z, range: range, current_id: id}])
end
是否有ActiveRecord这样做?
谢谢。这工作。我将它与上面的其他答案结合起来。 – GreenPlastik