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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:05:59+00:00 2026-06-13T08:05:59+00:00

The problem is that when a Restaurant does not have any MenuItems that match

  • 0

The problem is that when a Restaurant does not have any MenuItems that match the condition, ActiveRecord says it can’t find the Restaurant. Here’s the relevant code:

class Restaurant < ActiveRecord::Base
  has_many :menu_items, dependent: :destroy
  has_many :meals, through: :menu_items

  def self.with_meals_of_the_week
    includes({menu_items: :meal}).where(:'menu_items.date' => Time.now.beginning_of_week..Time.now.end_of_week)
  end
end

And the sql code generated:

Restaurant Load (0.0ms)←[0m  ←[1mSELECT DISTINCT "restaurants".id FROM "restaurants"
LEFT OUTER JOIN "menu_items" ON "menu_items"."restaurant_id" = "restaurants"."id"
LEFT OUTER JOIN "meals" ON "meals"."id" = "menu_items"."meal_id" WHERE
"restaurants"."id" = ? AND ("menu_items"."date" BETWEEN '2012-10-14 23:00:00.000000'
AND '2012-10-21 22:59:59.999999') LIMIT 1←[0m  [["id", "1"]]

However, according to this part of the Rails Guides, this shouldn’t be happening:

Post.includes(:comments).where("comments.visible", true)

If, in the case of this includes query, there were no comments for any posts, all the posts would still be loaded.

  • 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-13T08:06:00+00:00Added an answer on June 13, 2026 at 8:06 am

    The SQL generated is a correct translation of your query. But look at it,
    just at the SQL level (i shortened it a bit):

    SELECT *
    FROM 
      "restaurants"
    LEFT OUTER JOIN 
      "menu_items" ON "menu_items"."restaurant_id" = "restaurants"."id"
    LEFT OUTER JOIN 
      "meals" ON "meals"."id" = "menu_items"."meal_id" 
    WHERE 
      "restaurants"."id" = ? 
    AND 
      ("menu_items"."date" BETWEEN '2012-10-14' AND '2012-10-21') 
    

    the left outer joins do the work you expect them to do: restaurants
    are combined with menu_items and meals; if there is no menu_item to
    go with a restaurant, the restaurant is still kept in the result, with
    all the missing pieces (menu_items.id, menu_items.date, …) filled in with NULL

    now look aht the second part of the where: the BETWEEN operator demands,
    that menu_items.date is not null! and this
    is where you filter out all the restaurants without meals.

    so we need to change the query in a way that makes having null-dates ok.

    going back to ruby, you can write:

      def self.with_meals_of_the_week
        includes({menu_items: :meal})
           .where('menu_items.date is NULL or menu_items.date between ? and ?', 
                Time.now.beginning_of_week, 
                Time.now.end_of_week
            )
      end
    

    The resulting SQL is now

    .... WHERE (menu_items.date is NULL or menu_items.date between '2012-10-21' and '2012-10-28')
    

    and the restaurants without meals stay in.

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

Sidebar

Related Questions

I'm facing a problem that I can summarize as it follows: I have a
I have a big problem that a recursive treeview doesn't update any childs if
Simple problem that I can't figure out... How can I print a '%' character
The problem that I have is somehow very specific. I have to implement a
I have a problem that I'm working on for quite some time now. I
Here is a problem that has been bothering me a lot about Python, I
Seems to be a problem that many people have, but all the answers I
I have the same problem that is listed in the following thread. WSDL first
I have a list of restaurants that I want to match with the restaurants
I have application with 6 buttons on start screen. My problem is that I

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.