0

我了解到我无法在 ActiveRecord 中进行加入然后下订单。这似乎也适用于 pg_search 的 full_text_search 方法。例如,这不适用于我期望的排序:

self.full_text_search(search_params).where(where_string).order(order_string).page(page_number).per(items_per_page)

之后我必须 sort_by 吗?我基本上有一个可用的 order_array: ["question ASC", "answer DESC"] 并且可以这样做,但必须有更好的方法,不是吗?

4

1 回答 1

1

您可以在之后进行排序,您只需在分页时将其作为数组处理:

order_array = ["question ASC", "answer DESC"]
search_results = self.full_text_search(search_params).where(where_string)

begin
 order_array.reverse.each do |s|
   s_array = s.split
   s_by = s_array[0]
   s_order = s_array[1]
   search_results.to_a.sort!{ |x,y|
     if s_order.present? && s_order.downcase == 'desc'
       y[s_by] <=> x[s_by]
     else
       x[s_by] <=> y[s_by]
     end
   }
 end
 search_results = Kaminari.paginate_array(search_results).page(page_number).per(items_per_page)
rescue => e
 logger.error "Failed to sort results of full text search: #{e.message}"
 search_results.page(page_number).per(items_per_page)
end
于 2014-10-30T17:37:34.537 回答