Given a System model with three-dimensional coordinates x, y, and z, I have written the following instance method to give me all of the other Systems within a set range of the specific System in question. Here's the code:
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
Is there an ActiveRecord way of doing this?