我在视图中有一个<table>
,但它显示来自另一个模型的用户名 - Profile
。我为每个表格调用Profile。有两个问题:通过每次调用效率低下的配置文件调用&来暴露我的模型。Rails视图表显示来自另一个模型的值
是否有一种方法可以首先访问控制器中的所有用户名,并使用一个SQL查询,然后相应地只显示表中的每个值或更好的方法来显示没有表的值?
<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>
非常感谢。我是新手,没有找到任何示例。
谢谢Sedad。但用户表不具有full_name,只有配置文件表具有full_name。要获取每个应用程序的用户名,e_application.user将无能为力。我可以收集配置文件数组中的所有用户名,然后映射它吗? – Means