ruby-on-railshtml-tableadminwill-paginate

Rails will paginate table with multiple models


I'm building a small admin page for my app that will display data from 4 models in one table. The columns are: Clubs, Users, Posts, Comments.

A club has_many users, a user has_many posts and has_many comments. So my questions is do I need to add pagination explicitly to each of my 4 models in my admin_controller? The way it is now, I get the page list on the top and bottom of my table, and I can go back and forward pages, but all of my results are shown on the first page (~9000 results).

In my admin_controller I have

 @clubs = Club.all.paginate(page: params[:page], per_page: 50)

and in my view

<%= will_paginate @clubs %>
<table>
<% i = 0 %>
<tr class="new-admin-top-row">
  <td><%= "Club Location" %></td>
  <td>| <%= "Number of Signups "%> </td>
  <td>| <%= "Number of Posts By Users"%>    </td>
  <td>| <%="Number of Comments By Users"%> </td>
</tr>
    <%= @clubs.find_each do |club| %>
        <tr class="new-admin-row">
          <td class="new-admin-cell"><%= club.name %></td>
          <td class="new-admin-cell f"><%= @users_array[i] %></td>
          <td class="new-admin-cell s"><%= @posts_array[i] %></td>
          <td class="new-admin-cell"><%= @comments_array[i] %></td>
          <td class="new-admin-cell"><%= @elevates_array[i] %></td>
        <% i+=1 %>
      </tr>
  <% end %>
</table>
<%= will_paginate @clubs %>

Solution

  • The find_each method works on ActiveRecord::Relation objects and fetches 1000 records in batches. So that is where you problem most likely is. Change it to each and it'll probably solve your issue.

    You can read more about find_each here: http://apidock.com/rails/ActiveRecord/Batches/find_each