How can I order by a field and its value? For example, say I have 10 records, of which:

  • 2 records are active (status is active) and favorite is true
  • 2 records are active (status is active) and favorite is false
  • 3 records are completed (status is completed) and favorite is true
  • 3 records are completed (status is completed) and favorite is false

How can I put the records in order as above? That is,

  • active and favorite - true
  • active and favorite - false
  • completed and favorite - true
  • completed and favorite - false
有帮助吗?

解决方案

Let's suppose your model name is Restaurant.

If you are using ActiveRecord with rails, you may try this,

Restaurant.order('status, favorite DESC')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top