A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Wednesday, February 3, 2010

Rails, searchlogic and will_paginate

The searchlogic gem, for active record searching, integrates well with will_paginate, for pagination of the results in index pages, and it comes with simple helpers for setting up sortable column headers.

But I had trouble setting the default sort order for the results if not filtering had taken place or if no column header had been clicked. What I found on the web was a little confusing. Here's how it works (as of Feb 2010).

The model has a list of companies and I want the default search order to use their names.

In your controller:

@search = Company.search(params[:search])
@search.order ||= :ascend_by_name
@companies = @search.all.paginate :page => params[:page], :per_page => 20

Note that you set the order in @search and leave out the :order parameter to paginate.


Simple, elegant, love it...


 

2 comments:

Unknown said...

Thank you for that tip it help me a lot :)

Paul

datashaman said...

Thanks! Saved me some time! :)

Archive of Tips