Sort by default rails using MetaSearch

I use gem meta-search to provide some sorting features. The page is created_at ASC by default, but I want it to be created_at DESC , but I cannot explicitly specify this because it will override the MetaSearch sorting functions.

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

Any thoughts on how to achieve this?

+4
source share
3 answers

I had the same problem and decided to do it in the controller

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

The default sort order is created by DESC, but it will be overwritten if a new sort order is received in the parameters. Seems to work for me.

+9
source

Try this approach. This works for me:

 def index @search = Photo.search(params[:search]) @photos = @search.order("created_at desc") end 
+2
source

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

@ search.meta_sort = 'your_column.desc'

0
source

All Articles