简体   繁体   English

如何按优先级排序任务?

[英]How to sort tasks by priority?

In my schema, I have a table for tasks where one of the columns is priority. 在我的模式中,我有一个任务表,其中列之一是优先级。 In the task model I have assigned the priorities to an enumerable. 在任务模型中,我已将优先级分配给可枚举对象。 I am trying to sort them by high priority to low priority. 我试图按高优先级到低优先级对它们进行排序。 I have been able to sort them from low to high. 我已经能够将它们从低到高排序。 Please see my code and help me sort them the other way. 请查看我的代码,并以其他方式帮助我对它们进行排序。 Thanks. 谢谢。

class Task < ActiveRecord::Base

  belongs_to :project
  belongs_to :user
  belongs_to :assignee, class_name: "User", foreign_key: "assigned_user_id" 

  enum priority: ['whenever', 'low', 'medium', 'high', 'immediate']

  self.default_scope {order ('tasks.priority')}

So the answer was simply to add DESC to the query as such: 因此,答案很简单,就是将DESC这样添加到查询中:

self.default_scope {order ('tasks.priority DESC')}

Another way to do this is to add .reverse_order to the end of the query: 另一种方法是将.reverse_order添加到查询的末尾:

self.default_scope {order ('tasks.priority')}.reverse_order

Both produce identical results 两者产生相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM