如何在ASC中提供多个属性顺序

时间:2014-11-14 13:24:09

标签: ruby-on-rails ruby sql-order-by

我是ruby on rails的新手。我试图根据position数字以及row_order,row_order来显示任务,以便在拖放时设置任务的顺序。用于在任务上方添加的任务的位置,并在任务下方添加。对于row_order我正在使用ranked model gem

= render '/task_list', tasks: @project.tasks.incomplete.order( "position ASC, row_order ASC") 

我正在尝试这种方式,但它仅适用于第一个属性,如果它是positionrow_order

任务表

+------------------------------+
| id  |  row_order | position   |
+------------------------------+
| 1   | 1048577     | 1        |
| 2   | 7602176     | 2        |
| 3   | 8126464     | 3        |
+------------------------------+
当任务拖放时,

row_order会发生变化。当任务被拖动时position不会改变

拖动任务后

+------------------------------+
| id  |  row_order | position   |
+------------------------------+
| 1   | -2068477    | 1        |
| 2   |  7602176    | 2        |
| 3   | -5242879    | 3        |
+------------------------------+

1 个答案:

答案 0 :(得分:0)

以下是来源http://guides.rubyonrails.org/active_record_querying.html#ordering

的示例
Client.order(orders_count: :asc, created_at: :desc)
# OR
Client.order(:orders_count, created_at: :desc)
# OR
Client.order("orders_count ASC, created_at DESC")
# OR
Client.order("orders_count ASC", "created_at DESC")

或者你可以做

Client.order("orders_count ASC").order("created_at DESC")
# SELECT * FROM clients ORDER BY orders_count ASC, created_at DESC