繁体   English   中英

【Rails】如何将hash转成一个数组,每个都在erb中

[英]【Rails】How to turn a hash in an array with each in erb

想要达到

ruby 2.6.5
导轨 6.0.3

感谢您的关注。
我正在 Rails 中创建一个 web 应用程序。
我想知道您是否可以告诉我如何将多个表连接在一起,然后对每个表中的每个元素重复一次。

细节

从下表中,我们要获取与用户相关的任务以及与任务相关的信息(解决方案和拥有解决方案的公司名称),并将它们放在数组中的 hash 中。

我想 output 我们以这种方式创建的数组,一次一个,使用 erb 中的每个。

(我不知道如何在 EACH 中打开任务,但仍然 output 是公司名称和与 EACH 中的任务相关的其他信息。)

像这样

A suitable solution for mike task "task1
・ Company name:Dcompany Solution:「solution2」
・ Company name:Ccompany Solution:「solution3」

A suitable solution for mike task "task1
・ Company name:Acompany Solution:「solution1」

用户表

|id|name|
| 1|mike|
| 2|bob |

任务表

|id|task_name|user_id|
| 1|task1    |      1|
| 2|task2    |      1|
| 3|task3    |      2|

推荐表

|id|task_id|solution_id|
| 1|      1|          2|
| 2|      1|          3|
| 3|      2|          1|

解决方案表

|id|solution_name|company_id|
| 1|    solution1|         1|
| 2|    solution2|         4|
| 3|    solution3|         3|

公司表

|id|name    |
| 1|Acompany|
| 2|Bcompany|
| 3|Ccompany|
| 4|Dcompany|

试过了

首先,我能够加入如下所示的表格,将我想要的数据作为一个表格。
但是,我不知道如何从这里提取数据以及如何将其转过来,所以我放弃了。

tasks = user.eager_load(:tasks)
tasks = tasks.eager_load(:recommends)
tasks = tasks.eager_load(recommends: :solutions)
tasks = tasks.eager_load(recommends: {solutions: :companies })

这大约是我能够获得理想值的方式。

infos = []

users.each do |user|
  info = {}
  temporary_array = []
  user.companies.each do |company|
  end
  info[:detail] = temporary_array
  infos << info
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM