ruby-on-rails - Rails where, sort and combining 同一张表

标签 ruby-on-rails postgresql sorting where-clause

我有一个名为 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/

相关文章:

ruby-on-rails - ActiveRecord - 防止跨表重复条目

python - Django:关系 "django_site"在使用站点框架的 psql 应用程序中不存在

java - Android Robotium 排序测试器

php - 数组按索引变量排序

ruby-on-rails - 如何将自定义动词(http 请求方法)添加到 rails4

ruby-on-rails - Ruby Regex MatchData 对象捕获行为异常的方法

ruby-on-rails - Haml - 显式结束缓存

ruby-on-rails - 如何在 Rails 的 ActiveRecord 中获取对象的所有值?

postgresql - 使用 NestJS、TypeORM 和 Docker 运行数据库迁移时出错

javascript - 为什么 return -1 在 JavaScript 中将数组的第一个值排序在第二个值之前