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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:01:50+00:00 2026-05-21T22:01:50+00:00

I have created a has_many relationship where a Food can be served at several

  • 0

I have created a has_many relationship where a Food can be served at several different kinds of meals.
For instance, eggs can be served at a user’s breakfast or brunch.

That is, a Food is allowed to be served only at certain meals and this is tracked with a has_many relationship named meal_type.

Now suppose I have filtered my initial table by a certain query, such as:

@hot_foods = Foods.where(:is_hot => 1). 

And now I want to find out of the hot foods, only those that can be served at lunch.

Specifically, the relationships are given as follows:

class Food
has_many :meals, :through => :relationships
has_many :relationships

class Meal
has_many :foods, :through => :relationships
has_many :relationships

class Relationships
belongs_to :food
belongs_to :meals 

How would I perform this query?

  • 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-05-21T22:01:51+00:00Added an answer on May 21, 2026 at 10:01 pm

    To make the query easier, start with the Meal and work backwards:

    @meal = Meal.find_by_name('Lunch')
    @meal.foods.where('foods.is_hot' => true)
    

    Having properly defined has_many ..., :through relationships allows you do to this in a pretty straight-forward way.

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

Sidebar

Related Questions

I have created a simple has_many and belongs_to relationship between two activerecords and when
I have created a user control (link) which has a label and a button.
I have created like a design studio and it has different functionalities like drag
I have a store model that has many products with a has_many :through relationship.
I have two tables, users and groups . A user can belong to many
In my simple Rails 3 application I have a has_one, has_many relationship between two
I have a has_many relationship of models Role and Access through model Permission. I
I have a model Author with a has_many relationship to model Book. Books are
I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many
I've got two models, Users and Organizations, which have a has_many relationship using an

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.