I've learned that I cannot do a join and then an order in ActiveRecord. This appears to also apply to pg_search's full_text_search method. This, for example, does not apply the ordering I expect:
self.full_text_search(search_params).where(where_string).order(order_string).page(page_number).per(items_per_page)
Do I have to sort_by afterwards? I basically have an order_array available as such: ["question ASC", "answer DESC"] and could do that, but there must be a better way, no?
You could sort afterwards, you just have to handle it as an array when paginating:
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