Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:40:57+00:00 2026-06-16T05:40:57+00:00

In the tasks_controller, I defined: def index @tasks = Task.all respond_to do |format| format.html

  • 0

In the tasks_controller, I defined:

def index
    @tasks = Task.all

    respond_to do |format|
       format.html # index.html.erb
       format.json { render json: @tasks }
    end
end

I have three parameters in my table: name, task, done.

now, I want to produce:

<table>
    <tr>
        <th>Name</th>
        <th>num of tasks</th>
        <th>num tasks left</th>
    </tr>

    <% @tasks.group_by(&:name).each do |name, tasks| %>
        <tr>
            <td><%= name %></td>
            <td><%= tasks.size %></td>
            <td> here I want to produce the number of tasks left </td>
        </tr>
    <% end %>
</table>

how can I get the number of tasks left? I think it should be something like:

        <% @tasks.group_by(&:name).each do |name, tasks| %>
             <tr>
                <td><%= name %></td>
                <td><%= tasks.size %></td>
                <% @tasks.group_by(name, :done => "yes").each do |name, tasks| %>
                     <td><%= tasks.size %></td>
                <% end %>
             </tr> 
         <% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:40:58+00:00Added an answer on June 16, 2026 at 5:40 am

    You had a good idea 😉

    <% @tasks.group_by(&:name).each do |name, tasks| %>
      <tr>
        <td><%= name %></td>
        <td><%= tasks.size %></td>
        <td><%= tasks.select{ |task| task.done != 'yes' }.size %></td>
      </tr> 
    <% end %>
    

    tasks.select{ |task| task.done != 'yes' } : This will select the tasks where done is not yes, put it in an array and calculate its size (number of tasks not done yet).

    I strongly recommend you to set your @tasks variable like following (takes much less performances: the DB does the work for you!)

    # in the controller
    @tasks = Task.count(:group => 'name') #instead of @tasks = Task.all
    

    It has the same output as @tasks.group_by(...) but is much better in execution time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the call in the application.html.erb file: <%= render :partial => 'tasks/_new' %> Here's
I have the following code: tasks/_form.html.haml %h3 Tasks %span.form-inline.centered = form_for [@activity, @task], remote:
Projects have many tasks and a task has a custom RESTful action called 'approve'.
I defined method 'count' in my tasks controller as: def count @count = current_user.tasks.count
I'm having troubling completing a task the RESTful way. I have a tasks controller,
I have two controllers: workers and tasks. views/workers/index contains: <% @workers.group_by(&:name).each do |name, workers|
I have some custom methods defined in my task model. I'm sending them into
public class HomeController : Controller { public ActionResult Index() { Task<string> t = DownloadStringTaskAsync(new
I have a tasks table with a priority column, which has a unique constraint.
I have a list of tasks. Each tast has a day:date and a name:string.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.