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 8191453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:55:24+00:00 2026-06-07T03:55:24+00:00

Hopefully a simple question around a rails best-practice. Let’s keep this super simple; say

  • 0

Hopefully a simple question around a rails best-practice.

Let’s keep this super simple; say I have a task model that has an ID, description and status.

In my controller I have an index action to return all tasks

def index
  @tasks = Task.all
end

My question is, in my view, suppose I want to display the tasks in separate HTML tables according to their status.

What is the best practice?

a) Query the database multiple times in the index action, ie

def index
  @draft_tasks = Task.where(status: "Draft")
  @approved_tasks = Task.where(status: "Approved")
  @closed_tasks = Task.where(status: "Closed")
end

b) Query the database once, and filter in the contoller action

def index
  tasks = Task.all
  @draft_tasks = tasks.#somethinghere
  @approved_tasks = tasks.#somethinghere
  @closed_tasks = tasks.#somethinghere
end

c) Filter in the view

<% @tasks.each do |k, v| %>
  <% some if statement searching for the status I want %>
    # Some code to output the table
  <%end%>
<%end%>

or

d) Something else?

  • 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-07T03:55:25+00:00Added an answer on June 7, 2026 at 3:55 am

    The generally accepted best practices here are to keep controller methods thin and to keep logic out of the view. So with that in mind, one possible way to do this would be:

    # model
    class Task
      scope :drafts, where(:status => "Draft")
      scope :approved, where(:status => "Approved")
      scope :closed, where(:status => "Closed")
    end
    
    # controller
    def index
      @draft_tasks = Task.drafts
      @approved_tasks = Task.approved
      @closed_tasks = Task.closed
    end
    

    This will make 3 queries to the database, which could become a performance concern down the road, but if that does happen, you can optimize it at the model level (e.g. by defining class methods drafts, approved, and closed where the first one called prefetches everything). It’s less elegant though, so don’t prematurely optimize.

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

Sidebar

Related Questions

This is hopefully a simple question: I have an OpenGL texture and would like
hopefully a pretty simple question this time. I have a Select method in a
hopefully a simple question here, i have a barcode scanner using Zxing, this is
I have a (hopefully) simple question. Is it possible to use SSHD tunneling to
I'm just getting started with Require.JS and I have a (hopefully) simple question. I'm
Hopefully a simple question although one I have found impossible to answer myself using
Hopefully this is a quick question. I'm making a simple rock paper scissors game.
im struggling with syntax here: hopefully this question is v simple, im just miising
This is hopefully a very simple question, but one a lot of Googling has
this is quite a simple question hopefully. Our client currently has a Flash banner

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.