rails default sort using MetaSearch

1.2k views Asked by At

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?

3

There are 3 answers

0
Mark Gibson On

Try this approach. It works for me:

def index
  @search = Photo.search(params[:search])
  @photos = @search.order("created_at desc")
end
0
JNN On

@search = Photo.search(params[:search])

@search.meta_sort = 'your_column.desc'

1
Fredrik Boström On

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.