From f184f37b7c48623c301bc32ade796195604faadf Mon Sep 17 00:00:00 2001 From: Lannar Dean Date: Tue, 28 May 2024 14:15:14 -0500 Subject: [PATCH] simpify nearest_neighbors query when ORDER BY clause matches SELECT alias when the ORDER BY clause exactly matches the "AS neighbor_distance" select clause, simply use the neighbor_distance alias to simplify the query. Ultimately doesn't change the function of the query, but cuts the length in half which simplifies things when the query is being logged to log files etc. --- lib/neighbor/model.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/neighbor/model.rb b/lib/neighbor/model.rb index 33dd65c..a666c62 100644 --- a/lib/neighbor/model.rb +++ b/lib/neighbor/model.rb @@ -149,6 +149,8 @@ def self.neighbor_attributes order end + order = 'neighbor_distance' if order == neighbor_distance + # for select, use column_names instead of * to account for ignored columns select_columns = select_values.any? ? [] : column_names select(*select_columns, "#{neighbor_distance} AS neighbor_distance")