如何按枚举和其他列自定义顺序

How to custom order by enum and other columns

我正在尝试按两列排序。第一列是枚举字段(状态),第二列是 updated_at 列。

我目前正在按枚举排序,如下所示:

Car.order("
  (status = 1 DESC, updated_at DESC),
   status = 2 DESC,
   status = 3 DESC,
   status = 4 DESC,
   status = 5 DESC,
   status = 6 DESC,
   status = 7 DESC"
)

如果有 3 辆状态为 1 的汽车,我希望通过 updated_at 列降序对这 3 辆汽车进行排序。

根据评论

可以将SQL语句的ORDER BY传递给order查询方法:

Car.order(
  'status DESC,
   updated_at DESC,
   CASE WHEN status = 7 THEN 0
        WHEN status = 4 THEN 1
        ...
        END'
)