Checking whether a store is open or not by querying over its business hours I currently have a method that checks if the store is open. It's working nicely, however on the main page I have quite a few stores. Whom again have different locations. Each of these locations again have opening hours.

Now I'm not particularly good with SQL and DB querying and Skylight is complaining and telling me this can most likely be optimized. However I have no clue how.

This is taken from my Store model:

  has_many :business_hours, through: :locations
  ...

  def open?
    if current_business_hour
      true
    else
      false
    end
  end

  def current_business_hour(current_time: Time.current)
      business_hours.where('week_day = ? AND open_at &lt;= ? AND close_at &gt;= ?',
                           current_time.wday, current_time.to_s(:time), current_time.to_s(:time)).first
  end


I'm using Postgres. Now the code should be quite straight forward. I'm looking for business hours that match the current day, and that have a business hour that fits within the current time frame.

This generates:

SELECT  "business_hours".* FROM "business_hours" INNER JOIN "locations" ON "business_hours"."location_id" = "locations"."id" WHERE "locations"."store_id" = ? AND (week_day = ? AND open_at &lt;= ? AND close_at &gt;= ?) ORDER BY week_day LIMIT ?


I'm being notified that:

Skylight detected that this SQL query was repeated multiple times in a single request, which had a significant impact on its response time.

The stores are being rendered like this on a page:

&lt;ul&gt;&lt;%= render @stores %&gt;&lt;/ul&gt;


the _store.html.erb looks like this:

&lt;% if store.open? %&gt;
  &lt;li style="background-image: url(&lt;%= store.header.url %&gt;);"&gt;
    &lt;%= link_to store_path(store) do %&gt;
      &lt;h3&gt;&lt;b&gt;&lt;%= store.name %&gt;&lt;/b&gt;, &lt;%= store.city.name %&gt;&lt;/h3&gt;
      &lt;p class="open-store-text"&gt;Open&lt;/p&gt;
    &lt;% end %&gt;
  &lt;/li&gt;
&lt;% else %&gt;
  &lt;li style="background-image: url(&lt;%= store.header.url %&gt;);" class="store-closed"&gt;
    &lt;%= link_to store_path(store) do %&gt;
      &lt;h3&gt;&lt;b&gt;&lt;%= store.name %&gt;&lt;/b&gt;, &lt;%= store.city.name %&gt;&lt;/h3&gt;
    &lt;% end %&gt;
    &lt;p class="closed-store-text"&gt;Closed&lt;/p&gt;
  &lt;/li&gt;
&lt;% end %&gt;


Now my goal is to try and improve this.
Here's your problem:

&lt;ul&gt;&lt;%= render @stores %&gt;&lt;/ul&gt;

&lt;li&gt;&lt;% if store.open? %&gt;&lt;/li&gt;


You're rendering multiple stores via partials. When each partial is fully rendered out it looks like this:

&lt;ul&gt;
  &lt;li&gt;&lt;% if store.open? %&gt;&lt;/li&gt;
  &lt;li&gt;&lt;% if store.open? %&gt;&lt;/li&gt;
  &lt;li&gt;&lt;% if store.open? %&gt;&lt;/li&gt;
  &lt;li&gt;&lt;% if store.open? %&gt;&lt;/li&gt;
&lt;/ul&gt;


Each call to 

&lt;% if store.open? %&gt;


will result in a db query, which is where your n+1 problem is coming from.

You have 2 options:

1. Eager loading

#pseudocode
#controller
@stores = Store.includes(:business_hours).where(some_condition:...)

#partial
&lt;% if store.is_open?(store.business_hours) %&gt;

#model
def is_open?(business_hours)
   open = business_hours.detect {|h| (h.wday == Time.now.wday) &amp;&amp; ((h.open_time..h.close_time).cover?(Time.now)) }
   open.present? ? true : false
end


May get very memory intensive if you have thousands of stores and many business_hours for each store.

2. Denormalize your database and cache weekdays and business hours in your table

#pseudocode
#migration
table "stores"
   t.integer wday
   t.string open_time
   t.string close_time

#model
def is_open?
  (self.wday == Time.now.wday) &amp;&amp; ((self.open_time.to_time..self.close_time.to_time).cover?(Time.now))
end


The approach you take will depend on how many records you need to deal with at any one time.