Question

I have 2 models

  1. emp
  2. requisition

    emp has_many requisitions
    requisition belongs_to emp
    

In my console:

Requisition Load (1.0ms)  SELECT "requisitions".*FROM"requisitions" WHERE "requisitions"."emp_id" = $1  [["emp_id", 3]]=> #<ActiveRecord::Associations::CollectionProxy [#<Requisition id: 7, name: "Mo
bile", particular: "jskfbhksjfkjfgweh", unit: "LVP", quantity: 7, from: "Anil",
created_at: "2014-04-05 14:27:19", updated_at: "2014-04-05 14:27:19", total: 539, cost: 77, emp_id: 3>]>

When i apply in my rails app

 <div class="container">
<div class="row">
    <div class="col-md-9">
        <form class = "form-inline" action="#" method="get">
            <div class="input-group">
                <input type="text" class="form-control" id="system-search" placeholder="Search" name="q" required>
                <span class="input-group-btn">
                    <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
                </span>
            </div>
        </form>
    </div>
</div>
  <div class = "row">
      <div class="col-md-9">
        <table class="table table-striped table-bordered table-condensed">
          <thead>
              <tr style = 'background-color: white'>
                  <th> <i class="fa fa-male"></i> Username </th>
                  <th><i class="fa fa-bars"></i> Email</th>
                  <th><i class="fa fa-bars"></i>count</th>
                  <th><i class="fa fa-bars"></i>Time</th>
                  <th><i class="fa fa-bars"></i>Last time</th>
                  <th><i class="fa fa-envelope-o"></i>ip</th>
                  <th><i class="fa fa-map-marker"></i>last ip</th>
                  <th><i class="fa fa-phone"></i>TAKE actions</th>
            </tr>
          </thead>

        <tbody>
            <% @emp.each do |e| %>
                <tr >
                  <td><%= e.username %></td>
                  <td><%= e.email %></td>
                  <td><%= e.sign_in_count %></td>
                  <td><%= e.current_sign_in_at %></td>
                  <td><%= e.last_sign_in_at %></td>
                  <td><%= e.current_sign_in_ip %></td>
                  <td><%= e.last_sign_in_ip%></td>
                  <td><button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                      <%= e.requisitions.count %> Assets 
                      </button>

        </tbody>
    </table>
                  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                      <div class="modal-dialog">
                        <div class="modal-content">
                          <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                <h4 class="modal-title" id="myModalLabel"></h4>
                          </div>
                        <div class="modal-body">
                          <table class="table table-striped table-bordered table-condensed">
                            <tr style = 'background-color: white'>
                                <th> <i class="fa fa-male"></i> Asset name </th>
                                <th><i class="fa fa-bars"></i> particular</th>
                                <th><i class="fa fa-bars"></i>unit</th>
                                <th><i class="fa fa-bars"></i>quantity</th>
                                <th><i class="fa fa-bars"></i>Last cost</th>
                            </tr>                    

                            <tbody>
                              <tr>
                                <%= e.requisitions.map do |e| %>
                                  <%= e.name %>
                                  <% end %>
                              </tr>
                            </tbody>
                          </table>
                      </div>

                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      </div>
                    </div>
                  </div>
                </div>


             </td>
               </tr> <%end %>




    </div>
    </div>
   </div>

problem: instead of giving result it gives as

 <ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Requisition:0x3cd32c0>
Was it helpful?

Solution

You need quite a few changes in your code:

  1. Change

    <%= e.requisitions.map do |e| %>
       <%= e.name %>
     <% end %>
    

    To

    <% e.requisitions.each do |requisition| %>  
      <tr>
        <td><%= requisition.name %></td>
        <td><%= requisition.particular %></td>
         ...
      </tr>
     <% end %>
    

    Removed = and changed block variable to requisition. = was causing <ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Requisition:0x3cd32c0> part in your rendered output. You should not be rendering e.requisitions. You also need to surround the data with appropriate <tr> and <td> elements.

  2. As per the chat session, even after fixing point 1, your code was not behaving correctly. Every time you click on <%= e.requisitions.count %> Assets button assets of first employee were rendered even when you click on any other employees assets button. This happens because you are using the same id myModal for all the created divs created for modal functionality.

    So, when you click on the asset button, DOM would look for the first element with id myModal and return it. Hence, you get the details of 1st emp every time. In order to avoid this, you need to make unique id's for all your modal div's.

    Replace

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    

    With

    <div class="modal fade" id="myModal<%= e.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    

    This will create id of modal div element for emp with id 1 as : myModal1, emp with id 2 as : myModal2

  3. For change #2 to work correctly, you need to update the asset link as below:

    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal<%= e.id %>">
                      <%= e.requisitions.count %> Assets 
    </button>
    

    Due to data-target="#myModal<%= e.id %>", appropriate modal would be selected. For emp with id 1 as : myModal1 would be shown, emp with id 2 as : myModal2 would be shown

OTHER TIPS

This

e.requisitions

will return a collection of requisition objects: this is the ActiveRecord::Associations::CollectionProxy.... object. The erb tags will then call to_s on it to turn it into a string, to show on the web page. Which is what you're seeing.

What did you expect to see? If you want to see some data relating to the associated requisitions you will need to ask for it in the code. Since you've only got a td to play with there you may need to aggregate data from the associated requisitions into a single string (like you did on the .count td), or if you want to iterate over the requisitions you will need to rethink your table.

EDIT1 - response to poster's edit.

You are doing this:

<tr>
  <%= e.requisitions.map do |e| %>
    <%= e.name %>
  <% end %>
</tr>

This will generate an array of names and shove them into a tr. They will probably be concatenated together (depending on your version of ruby) and they will not be in a td. Not good. I think you want to do something like this: note that i'm guessing a bit at method names (particularly last_cost) so you will need to make sure it's correct. Hopefully you understand what the idea is anyway.

<table class="table table-striped table-bordered table-condensed">
  <tr style = 'background-color: white'>
      <th> <i class="fa fa-male"></i> Asset name </th>
      <th><i class="fa fa-bars"></i> particular</th>
      <th><i class="fa fa-bars"></i>unit</th>
      <th><i class="fa fa-bars"></i>quantity</th>
      <th><i class="fa fa-bars"></i>Last cost</th>
  </tr>                    

  <tbody>
    <% e.requisitions.each do |requisition| %>
      <tr>
        <td><%= requisition.name %></td>
        <td><%= requisition.particular %></td>
        <td><%= requisition.unit %></td>
        <td><%= requisition.quantity %></td>
        <td><%= requisition.last_cost %></td>
      </tr>
    <% end %>
  </tbody>
</table>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top