From 71bde3a249c76fe83602fa365cae8167589376b0 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Wed, 25 Feb 2026 22:12:14 -0500 Subject: [PATCH] Enhance search_result_ranks_and_ids method to rank results by ID and streamline query handling --- app/models/vehicle.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index 997a4ea..07280d0 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -85,6 +85,7 @@ class Vehicle < ActiveRecord::Base where("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%") end + # Override the defult redmine seach method to rank results by id # Override the defult redmine seach method to rank results by id def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {}) return {} if tokens.blank? @@ -92,14 +93,11 @@ class Vehicle < ActiveRecord::Base scope = self.all tokens.each do |token| - q = "%#{sanitize_sql_like(token)}%" - scope = where("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%") + scope = scope.search(token) end ids = scope.distinct.limit(options[:limit] || 100).pluck(:id) - - # rank by id - ids.each_with_object({}) { |id, h| h[id] = id } + ids.index_with { |id| id } end # returns a human readable string