我正在尝试使用 pg_search在我的 Rails 应用程序中,根据用户的查询对部件列表进行排序。不过,我遇到了一些麻烦 - 我似乎无法从升序搜索中获得正确的结果。
这是我的模型:
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
在我的 Controller 中,我有以下代码:
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
而不是order
。 重新排序
将删除其他地方定义的任何其他排序,以防出现问题。
关于ruby-on-rails - 使用 pg_search 和 Ruby on Rails 按字母顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61001206/