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

  • SEARCH
  • Home
  • 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 7718171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:06:00+00:00 2026-06-01T03:06:00+00:00

Background: I’m creating a dashboard as a project and I have a query that

  • 0

Background:

I’m creating a dashboard as a project and I have a query that I think is going to be a big performance issue:

<% for outlet in @outlets %>
    <% if Monitoring.where(:outlet_id => outlet.id).where('date(created_at) = ?', Date.today).exists? %>
        <li>
            <a class="done" href="<%= outlet_url(outlet) %>" rel="tooltip" title="<%= outlet.name %>"></a>
        </li>
    <% else %>
        <li>
            <a href="<%= outlet_url(outlet) %>" rel="tooltip" title="<%= outlet.name %>"></a>
        </li>
    <% end %>
<% end %>

What I’m trying to achieve is a series of dots on a page. If the anchor tag has a class of done, it will display as green, if not it will be red (done through CSS).

Aside from the obvious DRY issues here, this query is very heavy, so I’m looking at ways to improve it.

An Outlet is Monitored at least once a day (An Outlet has_many :monitorings). For each outlet I need to check if it has been monitored on that particular day, and output the HTML accordingly.

If anyone could help me with this it would be fantastic.
(Also, any advice on caching this would be appreciated).

Cheers in advance :).

  • 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-01T03:06:02+00:00Added an answer on June 1, 2026 at 3:06 am

    You might make a conditioned association for current monitors, then use includes to fetch the associated current monitorings on the original query.

    class Outlet
      has many :current_monitorings, :class_name => "Monitoring",
        :conditions => proc { [ 'monitorings.created_at > ?', Time.now.midnight ] }
    end
    
    @outlets = Outlet.includes(:current_monitorings)
    
    @outlets.each do |outlet|
      if outlet.current_monitorings.empty?
        # the no monitor today case
      else
        # it's been monitored today
      end
    end
    

    At the Postgres level, you’ll likely benefit from an index on monitorings(outlet_id,created_at) to support the outer join implied by #includes.

    BTW, it’s bad style to be executing database queries in your view. Put the domain logic in your models, and have your controller execute the query and supply the results to the presentation layer.

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

Sidebar

Related Questions

Background: I have a css and a js that is used only by the
Background of issue: I have a folder with lots of directories, files, attachments, and
Background This is only my second PyQt4 project. Developing a Windows app that has
Background I have a basic HTML page with an iframe that points to a
Background My project is urgent and requires that I iterate a large XML file
Background info: I have a windows application (in c#) that handles a custom file
Background: I have string of html with about 10 image tags that passes through
Background: I have a little video playing app with a UI inspired by the
Background: At my company we are developing a bunch applications that are using the
Background I have a massive db for a SharePoint site collection. It is 130GB

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.