Posts [ 4 ]

Topic: Sorting with associations and multiple columns

So I had a need to be able to sort a table and I didn't want to use all the fancy AJAX ways people have developed. I eventually came across this (rails super cool simple column sorting) and tried it out.

I ran into two problems I needed a way to sort through associations and I need to be able to sort by more than one column in certain cases.

For the first issue I found this (How do you order through associations?) on the forums. With some tweaking here and there I eventually came out with the following. I feel like it's a bit ugly and I know I'm still new to Ruby so I thought some experts could offer their tips.

Re: Sorting with associations and multiple columns

Yes Max that is what I was doing before. However, the sort_link helper method generates a link for the column title that allows the user to change what they sort the table by. The sort_order helper generates an SQL fragment based on which link was clicked. Most people do column sorting with AJAX but I didn't wish to go this route. The above method simply recalls the page and merges a few things in the params hash (c = column, d = direction)

I hope that clears up what I am try to do. Like I said it works fine just wanted to see if I should be doing anything better.