Question

I am following Ryan Bates' railscast on Sortable Table Columns and I have successfully gotten a column to sort ascending and descending.

My table is more complex than in the Railscast because I have columns from different tables.

# controller
@cars = Car.find(:all).order(sort_column + " " + sort_direction).includes(:manufacturers)


#view
<%= sortable "age" %>

How do you add sortable columns for related tables such as manufacturers?

Was it helpful?

Solution

I had a similar issue. Got it fixed with something like:

 <%= sortable "manufacturers.name", "Manufacturer name" %>
 <%= sortable "cars.age", "Age" %>

The sort function in the application_controller should be something like this:

 def sort_column
     ['manufacturers.name', 'cars.age'].include?(params[:sort]) ? params[:sort] : 'cars.age'
 end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top