Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails default sort using MetaSearch

I am using the gem metasearch to provide some sorting features. The page is defaulting to a sort of created_at ASC, but I want it to be created_at DESC, but I can't explicitly state that because it will override MetaSearch's sort features.

def index
  @search = Photo.search(params[:search])
end

Any thoughts on how to achieve this?

like image 708
Tony Beninate Avatar asked Dec 22 '22 07:12

Tony Beninate


2 Answers

I had the same problem and ended up doing like this in the controller

search = {"meta_sort" => "created_at.desc"}.merge(params[:search] || {})
@search = Photo.search(search)

Default sort order is created_at DESC, but it will be overwritten if a new sort order is received in the params. Seems to work for me.

like image 110
Fredrik Boström Avatar answered Jan 09 '23 08:01

Fredrik Boström


Try this approach. It works for me:

def index
  @search = Photo.search(params[:search])
  @photos = @search.order("created_at desc")
end
like image 36
Mark Gibson Avatar answered Jan 09 '23 09:01

Mark Gibson