我在視圖中有一個<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