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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:59:01+00:00 2026-06-14T04:59:01+00:00

I am new to rails, and Im having hard time writing a query using

  • 0

I am new to rails, and Im having hard time writing a query using rails’ methods.

What I am trying to accomplish is to have a Canteen model that has many Meals, however I want to only display meals that are being served that day.

So I created a Canteen model, that has_many Meals.
Then I created Served_date model, that belongs_to meal and Meal has_many Served_dates so we can specify multiple dates when the meal is being served.

How would I make query like this?

//P.S.: Served_date.where(served_at: Date.today) returns Served_dates that are being served today

Thanks !

  • 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-14T04:59:03+00:00Added an answer on June 14, 2026 at 4:59 am

    Firstly, the model name should not be underscored by convention, hence you should use ServedDate instead of Served_date.

    This query should give you what you need:

    Meal.joins(:served_dates).where(served_dates: {served_at: Date.today})
    

    Better yet, you can parameterize it and turn it into a scope to be more portable:

    class Meal < ActiveRecord::Base
      scope :served_on, lambda{|date| joins(:served_dates).where(served_dates: {served_at: date}) }
    end
    

    And then call:

    Meal.served_on(Date.today)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to Rails and having a hard time envisioning how to proceed with a
I'm new to Rails and having a hard time figuring out how to create
I am new to rails and I having trouble using cucumber to run my
I having a hard time to consume this webservice https://www.arello.com/webservice/verify.cfc?wsdl in my rails application.
I'm new to rails and having issues with scope. I have two classes, Post
I'm having hard time debugging the flow of the code on Ruby on Rails.
I am having problems building a new Rails app. When trying to run @invoice.total
I am new to rails. Having a blast. The query API though is giving
Sorry for the vague question title, but I'm having a hard time trying to
I'm completely new to rails and I'm having a lot of trouble getting my

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.