0

I have a <table> in a view but it's showing username from another model -Profile. I'm calling Profile for each row of table. There are two problems with this: exposing my model in view by calling it & each time calling Profile which is inefficient.

Is there a way to access all usernames in controller first with one SQL query and then display only each value in table correspondingly or a better approach to show values without table?

           <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>

Many thanks. I'm new to rails and not found any example.

2 Answers 2

1

For start don`t do

Profile.find_by_user_id(e_application.user_id).full_name

If you did relations correctly just call

e_application.user.full_name

In case you don`t always have user

e_application.try(&:user).try(&:full_name)

use this so you don`t get error.

Single dot notations is always good but for this example no need to complicate things.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks Sedad. but user table doesnot have full_name and only profile table has full_name. To get username for each application, e_application.user will not help. can I collect all usernames in profile array and then map it?
0

First of all, include the profile table in the employee list.

For eg.

I am assuming that

employee

 belongs_to :user 

user

 has_one :profile

then,

employee = Employee.includes(user: [:profile], :application).where(your condition)

then simply display as below:

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

2 Comments

Thx, Chakreshwar but I didnot follow how to include profile in user...employee is an enum role in user.
I'm not sure if it's possible to somehow get all full_name from profile using a where query in an array or a hash in e_application controller and use that array/hash to map it to each table row. And when I display I will have to map that hash value for a table row using user_id as key. The part to display looks complex to fetch from hash. any ideas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.