I have a priority field with four options: “Urgent”, “High”, “Normal”, and “Low”.
I have this field displayed in a table of records, and I am using will_paginate’s ordering as follows:
@projects = @company.projects.paginate :page => params[:page], :order => (sort_column + " " + sort_direction)
private
def sort_column
['priority', 'name'].include?(params[:sort]) ? params[:sort] : "priority"
end
A typical ordering as above will give me:
- High
- Low
- Normal
- Urgent
This order is wrong. How can I get this column to order specifically to Urgent, High, Normal, Low?
Not sure if Rails has an easier way to do this, but the way I typically see it done is to have a separate table holding your priorities along with an order field that’s just an integer. Then you just join to that table and order by the integer field.