Bradley
Bradley

Reputation: 932

for each loop help in Rails. Trying to count data in each table.

Quick one. How can I pull in the number of courses that are completed to show in a for each block in the a view. Currently as the code loops through all the categories it only brings in the category_id result based on my hard code of 1. How can I get this loop to pull in the category Id for each of the categories in my block code so that i can count only courses showing as not complete in my view. I've shown the controller and view code below. The parts that are related. Thanks.

View loop

   <% @categories.each do |category| %>
<div class="row">
        <!-- <a href="/courses/categories"> -->
             <!-- <#= link_to "See Cat", courses_categories_path(category_id: category.id), class: "cat-item-heading", method: :post %> -->
                <div class="slide-down-delay col-md-3 col-sm-6 col-xs-12">
                        <div class="box">
                                <div class="item-remain original">
                                        <!-- <p class="cat-item-heading">health care</p> -->
                                        <% if category.name.length < 20 %>
                                            <p class="cat-item-heading"><%= category.name %></p><br>
                                        <% else %>
                                            <p class="cat-item-heading"><%= category.name %></p>
                                        <% end %>

                                    <br><p class="cat-item-count"><%= @categories_active.count %></p><br>
                                        <% if @categories_active.count > 1 || @categories_active.count < 1 %>
                                            <p class="cat-item-type">Courses available</p>
                                        <% else %>
                                            <p class="cat-item-type">Course available</p>
                                        <% end %>
                                </div>
                                <div class="overlay">
                                     <%= link_to "See All", courses_categories_path(category_id: category.id), class: "cat-item-heading no-link-style", method: :post %>
                                        <!-- <p class="cat-item-heading">see all</p> -->
                                        <!-- <#= link_to "", courses_categories_path, class: "cat-item-heading" %> -->
                                </div>
                        </div>
                </div>
        <!-- </a> -->
 <% end %>

Controller variable being pulled from DB

@categories = Category.all.order("created_at ASC").limit(12)
@categories_active = Course.where(category_id: 1)
@categories_active = @categories_active.where(complete: false)

Any direction appreciated. Currently it just counts all courses in the category_id 1 and displays the same count across all the categories shown. I'm trying to extract the category_id out in the loop or controller so I can display a count for each of the courses that are false in each category in the loop. Thanks.

Upvotes: 0

Views: 308

Answers (1)

Junan Chakma
Junan Chakma

Reputation: 651

I think you can get the count number for each of the courses that are false in each category by adding like this in your view

   <% @categories.each do |category| %>
      .....
      Uncompleted course count number for <%= category.name %> category: <%= category.courses.where(complete: false).count %>
      .....

   <% end %>

Upvotes: 2

Related Questions