rustyshackleford
rustyshackleford

Reputation: 731

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. I've run into some trouble, though -- I can't seem to get correct results from an ascending order search.

Here's my 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:

Pcdb::Part.search_for_parts(query) where the query parameter is input from a user.

I've tried using query.order("parts.part_terminology_name ASC") to no avail.

Right now, this is the result I get if the query parameter is "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!

Upvotes: 3

Views: 860

Answers (1)

ellitt
ellitt

Reputation: 833

Try using reorder instead of order. reorder will remove any other ordering defined elsewhere in case that's throwing things off.

Upvotes: 6

Related Questions