Ruby on rails Rails视图表以显示来自其他模型的值

Ruby on rails Rails视图表以显示来自其他模型的值,ruby-on-rails,forms,html-table,views,Ruby On Rails,Forms,Html Table,Views,我的视图中有一个,但它显示的是另一个型号的用户名-配置文件。我正在为每一行表调用Profile。这有两个问题:通过调用模型在视图中公开模型&每次调用Profile都是低效的 是否有一种方法可以首先通过一个SQL查询访问controller中的所有用户名,然后相应地只显示表中的每个值,或者有一种更好的方法来显示不带表的值 <table class="table table-bordered table-striped"> <thead

我的视图中有一个
,但它显示的是另一个型号的用户名-
配置文件
。我正在为每一行表调用Profile。这有两个问题:通过调用模型在视图中公开模型&每次调用Profile都是低效的

是否有一种方法可以首先通过一个SQL查询访问controller中的所有用户名,然后相应地只显示表中的每个值,或者有一种更好的方法来显示不带表的值

           <table class="table table-bordered table-striped">
            <thead>
            <tr>
              <th> Applicant Name </th>
              <th> Email </th>
              <th> Documents </th>
            </tr>
            </thead>
            <tbody>
              <% employee.eager_load(:application).each do |e_application| %>
                <tr>
                  <td><%= Profile.find_by_user_id(e_application.user_id).full_name %></td>
                  <td><%= mail_to(e_application.applicant.email) %></td>
                  <td><%= link_to e_application.doc.file.basename, e_application.doc_url if !eapplication.doc.file.nil? %></td>  
                </tr>
              <% end %>
            </tbody>
          </table>

申请人姓名
电子邮件
文件
非常感谢。我是rails新手,没有找到任何示例。

对于start,不要这样做

Profile.find_by_user_id(e_application.user_id).full_name
如果你做得对,就给我打电话

e_application.user.full_name
如果你不总是有用户

e_application.try(&:user).try(&:full_name)
使用此选项,以免出错


单点符号总是好的,但对于本例来说,不需要使事情复杂化。

首先,在员工列表中包括
profile

例如

我假设

雇员

 belongs_to :user 
使用者

那么

然后简单显示如下:

 <% employee.each do |e_application| %>
                <tr>
                  <td><%= e_application.user.profile.full_name %></td>


谢谢你。但用户表没有全名,只有配置文件表有全名。要获取每个应用程序的用户名,e_application.user将不会提供帮助。我可以收集配置文件数组中的所有用户名,然后映射它吗,Chakreshwar但我没有了解如何在用户中包含配置文件…employee是用户中的枚举角色。我不确定是否有可能使用数组中的where查询或e_应用程序控制器中的哈希从配置文件中获取所有全名,并使用该数组/哈希将其映射到每个表行。当我显示时,我必须使用user_id作为键映射表行的散列值。要显示的部分从哈希中提取看起来很复杂。有什么想法吗
employee = Employee.includes(user: [:profile], :application).where(your condition)
 <% employee.each do |e_application| %>
                <tr>
                  <td><%= e_application.user.profile.full_name %></td>