will_paginate - 高度具体的排序
我有一个 priority
字段,其中有四个选项:“紧急”、“高”、“正常”和“低”。
我将此字段显示在记录表中,并且我正在使用 will_paginate 的排序,如下所示:
@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
上面的典型排序将给出:
- 高低
- 正常
- 紧急
- 此顺序是错误
的。我如何才能将此列专门排序为“紧急”、“高”、“正常”、“低”?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定 Rails 是否有更简单的方法来做到这一点,但我通常看到的方法是使用一个单独的表来保存您的优先级以及一个整数的订单字段。然后,您只需加入该表并按整数字段排序即可。
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.
我建议您看一下这个截屏视频。
它对数据列进行排序非常容易实施。
希望对您有帮助。
I would like you suggest to take a look at this screencast.
It does the sort data columns very easy to implement.
Hope it helps you.
是的,您的数据库正在按字典顺序对优先级进行排序。最简单的解决方案是将这些字符串优先级映射到整数优先级并对其进行排序 - 正如 @jdc 建议的那样。
我可能倾向于避免将优先级存储在单独的表中,这样您就不必进行联接,而只需将数字优先级存储在新列中。
但想法是一样的。
Yes, your DB is sorting the priority in lexicographic order. The easiest solution is to map those string priorities to integer priorities and sort on that - as @jdc suggested.
I'd probably tend to stay away from storing the priorities in a separate table just so you dont have to do a join, and just store the numerical priority in a new column.
But the idea is the same.