使用 pg_search 和 Ruby 在 Rails 上按字母顺序排序

Sort by alphabetical order using pg_search with Ruby on Rails

我正在尝试在我的 Rails 应用程序中使用 pg_search 根据用户的查询对零件列表进行排序。不过,我 运行 遇到了一些麻烦 -- 我似乎无法从升序搜索中获得正确的结果。

这是我的模型:

class Pcdb::Part < CatalogsRecord
  include PgSearch
  self.table_name = "parts"
  belongs_to :parts_description
  pg_search_scope :search_for_parts, against: :part_terminology_name,
                   using: { tsearch: { dictionary: "simple", prefix: true } }
end

在我的控制器中,我有以下代码:

Pcdb::Part.search_for_parts(query) 其中 query 参数由用户输入。

我试过使用 query.order("parts.part_terminology_name ASC") 无济于事。

现在,如果 query 参数是 "Windshield Wiper":

,这就是我得到的结果
Windshield Wiper Motor And Windshield Washer Pump Assembly
Windshield Wiper Motor Relay
Windshield Wiper Switch
Windshield Wiper Motor
Windshield Wiper Arm
Windshield Wiper Blade
Windshield Wiper Linkage
Windshield Wiper Linkage Pivot
Windshield Wiper Blade Refill

我实际上正在努力实现这样的结果:

Windshield Wiper Arm
Windshield Wiper Blade
Windshield Wiper Blade Refill
Windshield Wiper Linkage
Windshield Wiper Linkage Pivot
Windshield Wiper Motor
Windshield Wiper Motor And Windshield Washer Pump Assembly
Windshield Wiper Motor Relay
Windshield Wiper Switch

我很感激被指出正确的方向!

尝试使用 reorder 而不是 orderreorder 将删除其他地方定义的任何其他顺序,以防出现问题。