ruby-on-rails - 如何在 Rails 中包含查询中的多个订单
问题描述
我有两个模型:Question
和Answer
. 问题 - 答案关系是1-n
我的问题和答案都有order
属性,现在我需要按顺序列出问题及其答案。
某事喜欢:
Question.includes(:answers).order(questions: {order: :asc}, answers: {order: :asc})
任何帮助都非常受欢迎!
解决方案
has_many
您可以在模型中指定订单。
# app/models/question.rb
# This will order answers by date, you presumably want something else
has_many :answers, -> { order(date: :desc) }
推荐阅读
- cordova - 将自定义构建/准备步骤添加到项目
- javascript - 当我单击粗刷按钮时,如何使用 javascript 在 html 中删除画布中的线宽
- elasticsearch - elasticsearch - 查找特定字段中的所有单词
- asp.net-core - 在 Windows Server 上调用 ASP .Net Core 2.1.7 微服务时出现“500 - 内部服务器错误”
- python - 将密码保存在 .txt 文件中,以便稍后替换(如果已存在)
- oracle-apex - 从更改选择列表的动态操作中调用模式窗口
- tensorflow - 在带有 TF 的 Keras 中使用多个 TPU
- google-apps-script - 使用 Google Apps 脚本在 Google Doc 中自动生成雷达图
- php - PHP 到 PDF 勾选复选框
- python - 如何用模板标签复数