我有一个名为 extra
的模型。这是它的样子;
#<Extra id: nil, name: nil, price: nil, per: nil, compulsory: nil, online_payment: nil, payment_per_person: nil, is_included: nil, created_at: nil, updated_at: nil, user_id: nil>
我想首先按 is_included
对它们进行排序,如果是的话。然后我想对 compulsory
为真的地方进行排序,最后对 compulsory
为假的地方进行排序。然后我想把它们结合起来。
基本上,我想将它们分组,因为用户可以无序地保存它们。如果有人存在两次,则删除。
我应该在 View 或 before_save 回调中执行吗?
谢谢
最佳答案
Extra.order('is_included desc, compulsive desc')
将返回您想要的结果,您永远不应在 View 上执行 SQL 查询。
关于ruby-on-rails - Rails where, sort and combining 同一张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45979261/