简体   繁体   English

如何从多个表中获取数据并将它们与erb中的每个表一起转换

[英]How to get data from multiple tables and turn them with each in erb

Want to achieve想要达到

ruby 2.6.5 rails 6.0.3 ruby 2.6.5 导轨 6.0.3

Thank you for your interest.感谢您的关注。 I am creating a web application with rails.我正在创建一个带有导轨的 web 应用程序。

I would like to know how to extract only the data of the columns I want from multiple tables, put them into an array, and then output them using each.我想知道如何只从多个表中提取我想要的列的数据,将它们放入一个数组中,然后使用每个 output 它们。

Details细节

I want to extract the relevant users.name, companies.company_name, and tasks.task_name from the following table and put them into an array as a single unit.我想从下表中提取相关的 users.name、company.company_name 和 tasks.task_name,并将它们作为一个单元放入一个数组中。 I want to output the array we have created in this way, one at a time, using each in erb.我想 output 我们以这种方式创建的数组,一次一个,使用 erb 中的每个。

Like this像这样

・john Acompany task3
・white Dcompany task12
・carl Bcompany task8
・mark Ccompany task97

tables

users table用户表

id ID name姓名 sex性别 company_id company_id
1 1 john约翰 1 1 1 1
2 2 white白色的 1 1 4 4
3 3 carl卡尔 1 1 2 2
4 4 mark标记 1 1 3 3

companies table公司表

id ID company_name公司名称 industry_1行业_1 industry_2行业_2
1 1 Acompany一家公司 3 3 9 9
2 2 Bcompany公司 6 6 10 10
3 3 Ccompany公司 1 1 3 3
4 4 Dcompany公司 4 4 8 8

tasks table任务表

id ID task_name任务名称 company_id company_id
1 1 task3任务3 1 1
2 2 task12任务12 4 4
3 3 task97任务97 3 3
4 4 task8任务8 2 2

Tried试过了

I thought that I could pack each element into an array as shown below.我认为我可以将每个元素打包成一个数组,如下所示。 But I couldn't imagine how to implement it, so I gave up.但是我无法想象如何实现它,所以我放弃了。

user_name = []
users.each do |user|
  user_name << user.name
end 

You can use thejoins method from ActiveRecord and get the columns you're looking for with pluck :您可以使用 ActiveRecord 中的joins方法,并使用pluck获取您要查找的列:

user_details = User.joins(company: :tasks).pluck("users.name", "companies.company_name", "tasks.task_name")
#=> [["john", "Acompany", "task3"], ["white", "Dcompany", "task12"], ...]

Next, iterate over the array of arrays that was returned from the above code to display the data in the way you want.接下来,遍历从上述代码返回的 arrays 数组,以您想要的方式显示数据。 Keep in mind in that in your DB architecture a company has_many tasks so you may end up with several arrays for the same company if that company has more than one task.请记住,在您的数据库架构中,一家公司有多个任务,因此如果该公司有多个任务,您最终可能会为同一家公司获得多个has_many

user_details.each do |user_detail|
  puts "・ " + user_detail.join(" ")
end

This is not a good idea.这不是一个好主意。 By passing arrays from your controller to the view you're skipping the model layer completely and skipping all it provides in terms of desearialization and encapsulation (and more).通过将 arrays 从 controller 传递到视图,您将完全跳过 model 层并跳过它在反序列化和封装方面提供的所有内容(以及更多)。 Dealing with simple arrays also destroys the readability of the code:处理简单的 arrays 也会破坏代码的可读性:

# which one of these was what now?
<tr>
  <td><%= data[0] %></td>
  <td><%= data[1] %></td>
</tr>

It is after all a MVC framework built in an object oriented language.毕竟它是一个用 object 面向语言构建的 MVC 框架。 Your code should not look like that.你的代码不应该是这样的。

Instead you can either use eager loading and delegation:相反,您可以使用预先加载和委托:

class User < ApplicationRecord
  belongs_to :company
  delegate :name, to: :company, prefix: true
end
@users = User.eager_load(:company)
             .all
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Company</th>
    </tr>
  </thead>
  <tbody>
    <%= @users.each do |user| %>
    <tr>
      <td><%= user.name %></td>
      <td><%= user.company_name %></td>
      # ...
     </tr>
  </tbody>
<% end %>

In some cases you may want to use joins instead to just select a single column or an aggregate from the assocation through:在某些情况下,您可能希望使用joins来代替 select 单个列或来自关联的聚合:

class UsersController
  def index
    @users = User.joins(:company)
                 .select(
                   'users.*', 'companies.name AS company_name'
                  )
  end
end

When it comes to tasks you have to figure out what it is that you want to actually display.当涉及到任务时,您必须弄清楚您想要实际显示的是什么。 Since the assocations is most likely many to many to both users and companies its not clear which task should even be listed.由于关联对于用户和公司来说很可能是多对多的,因此甚至不清楚应该列出哪个任务。 All of them?他们全部? The first?首先? The last?最后?

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

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