简体   繁体   English

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

[英]Sort by alphabetical order using pg_search with Ruby on Rails

I'm trying to use pg_search in my Rails application to sort a list of parts based on a query from a user.我试图在我的 Rails 应用程序中使用pg_search根据用户的查询对部件列表进行排序。 I've run into some trouble, though -- I can't seem to get correct results from an ascending order search.不过,我遇到了一些麻烦——我似乎无法从升序搜索中获得正确的结果。

Here's my model:这是我的 model:

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

In my controller, I have the following code:在我的 controller 中,我有以下代码:

Pcdb::Part.search_for_parts(query) where the query parameter is input from a user. Pcdb::Part.search_for_parts(query)其中query参数是从用户输入的。

I've tried using query.order("parts.part_terminology_name ASC") to no avail.我试过使用query.order("parts.part_terminology_name ASC")无济于事。

Right now, this is the result I get if the query parameter is "Windshield Wiper":现在,如果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

I'm actually trying to achieve a result like this:我实际上正在尝试实现这样的结果:

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

I'd appreciate being pointed in the right direction!我很感激被指出正确的方向!

Try using reorder instead of order .尝试使用reorder而不是order reorder will remove any other ordering defined elsewhere in case that's throwing things off. reorder将删除其他地方定义的任何其他排序,以防万一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM