4

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?

Tony Beninate
  • 1,926
  • 1
  • 24
  • 44

3 Answers3

9

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.

Fredrik Boström
  • 1,499
  • 1
  • 19
  • 22
  • 2
    cleaner way as for me `params[:search] ||= { 'meta_sort' => 'created_at.desc' } @search = Photo.metasearch(params[:search])` – Rustam Gasanov Sep 16 '13 at 13:12
2

Try this approach. It works for me:

def index
  @search = Photo.search(params[:search])
  @photos = @search.order("created_at desc")
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0

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

@search.meta_sort = 'your_column.desc'

JNN
  • 947
  • 10
  • 19