ruby-on-rails - 如何在rails关联中打印多个表格的数据?
问题描述
我已经创建了项目、阶段、任务和 sub_task 脚手架。项目与阶段有一对多关联,阶段与任务有一对多关联,任务与子任务有一对多关联。我想在 project#show 中渲染每个任务的所有 sub_task,目前我能够将所有 sub_tasks 渲染到每个任务。
路线.rb
resources :projects do
resources :stages do
resources :tasks do
resources :sub_tasks
end
end
end
项目控制器.rb
def show
@project = Project.includes(stages: :tasks).find(params[:id])
@stages = @project.stages
@sub_tasks = SubTask.all
end
解决方案
您可以包括subtasks
以下tasks
内容:
def show
@project = Project.includes({stages: {tasks: :sub_tasks}}).find(params[:id])
@stages = @project.stages
# Now when you iterate through stages, you can fetch tasks associated with each stage, and for each task, you can get subtasks. All of this happens without additional DB queries because of the "includes"
end
这将获取与项目相关的所有阶段,与每个阶段相关的所有任务,然后是与每个任务相关的子任务!
推荐阅读
- javascript - 如何使用谷歌表格作为后端数据库向外部网页提供信息
- azure-devops - 与 JIRA 的深度链接
- c# - Net Core 2 相当于 ChildActionOnly
- python - 具有多个数据库的 SQLAlchemy 和由于重复索引创建而导致的错误
- tableau-api - 在 Tableau 中计算滚动日期范围之间的记录
- sql - CopyFromRecordset 在 Server 2012 中失败,但在 Windows 7 或 VBA/Excel 中的 Windows 10 中没有
- karate - 空手道正则表达式给出错误 - 原因:不是字符串
- python - Flask CLI 在通过 docker-compose 运行时抛出 'OSError: [Errno 8] Exec format error'
- java - 如何开始为 JHipster 做贡献
- c# - 如何从 Fody/Costura 中排除文档文件?