Sortable Columns Across Tables

If you follow Railscast 228 you get sortable table columns for your model. But what if you don’t want to expose the name of the actual database columns or more interesting if you want to sort across tables? Here is how I do it.

In your controller add order or reorder if you already have an order clause in one of the used scopes (default_scope counts too).

As I’m using this mechanism in different controllers I added the common functionality to the application_controller.rb file.

This will use the ALLOWED_SORT_COLUMNS hash to map between user visible and actual database sort columns. Adding sort_query also allows us to sort by multiple columns at once. navigation_params is a shortcut I use when generating URLs (e.g. in link_to) and I want to preserve pagination, sorting, filters/searches, etc. across pages.

Note that sort_icon assumes you are using Bootstrap.

Now we can have sortable columns in our views:


I was using ZSH for quite a while now instead of Bash. But thanks to Ryan Bates’ screencast about Oh My ZSH I finally came around to properly configure it and use it to its full potential.

I immediately created my own theme and was able to extract custom plugins out of my previous configuration. One of them might even end up in a pull request. 🙂 I used the custom plugins mechanism to split up and spread my configurations across all of my machines. So now I have a consistent set of configurations and I can activate them individually according to each machine’s purpose (i.e. no need for KDE specific settings on a server 😉 ).

A nice productivity and consistency boost. 😀