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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:38:27+00:00 2026-05-21T17:38:27+00:00

I got into a dilemma about using has_many, :through. Suppose I am trying to

  • 0

I got into a dilemma about using has_many, :through.

Suppose I am trying to model a restaurant’s menu through Rails. And suppose this restaurant can have several different kinds of menus, depending on the kind of the meal.

For instance, we would have a menu for the meal type breakfast, a menu for lunch, and so forth. And we would also like to add functionality for adding a kids menu and adults menu, etc.

Clearly, we should have a Menu and a Food class, we also need a join table for these to specify the different menus and the HABTM relationship between them.

But how do we deal with the Meal Type?
Note that a Meal Type belongs_to many Foods, but only belongs to one Menu. I feel that adding it to the previous join table would be inefficient.

In this example, I think I would solve by creating a new model, MealType, and writing that a Food has_many :meal_types, :through meals.

Then updating the Menu model, since a Menu can only have one meal type. This means I would have a has_one relationship with MealTypes for each menu. Briefly:

Foods class:

has_many Menus, :through menu_food_join_table  
has_many MealTypes, :through meals  

Menu class:

has_many Foods, :through menu_food_join_table  
has_one MealType  

MealType class:

belongs_to Foods, :through meals  
belongs_to Menus  

And here I arrived at my dilemma: this code stinks, at least to me. It doesn’t seem practical to have several tables for a simple relationship as this. And I don’t think I should add a MealType field to Foods, since some foods could be on several meals (like Coke).

Neither should I add a MealType to Menus, since I want to be able to separate foods by meal types as well (if I wanted to show only the foods that are allowed at certain meals, for instance).

What would you do in this case? I must confess I don’t have a lot experience with OO design, which is why I came here.


My final solution

Here is the solution I chose to solve the problem:

Since my real goal was to generate menus algorithmically, and since in this work I don’t have to save any of the generated menus, I destroyed the Menu model and created a Meal model.

Then I added a has-many Meals relationship to the Food model, through a join table. The only thing in the Meal database are the types of meals I want to be able to create (breakfast, lunch, dinner, etc). A meal has-many Foods through the same join table.

I still have the Menu controller and view, which I use to create a MenuFactory.

This MenuFactory is a standard factory pattern, which takes in the number of days I want to generate the menu for and the specific algorithm for the restaurant (for instance, the restaurant only serves plates with less than 300 calories) and spits out the entire menu. The rest is simple programming.

  • 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-21T17:38:27+00:00Added an answer on May 21, 2026 at 5:38 pm

    You seem to be overcomplicating things. You have one restaurant then it has one breakfast menu, one lunch menu, one dinner menu. That’s just three menus – I don’t think you need to model the type at all.

    So really you just need

    Menu:
    has_many menu_items

    MenuItem:
    belongs_to menu

    I suppose you could argue that a menu item could possibly be on more than one menu, like having breakfast items on the lunch menu – but if you don’t need to get that complicated then don’t.

    If what you’re trying to do with the menu is more complicated than that then I would suggests your main problem is that you haven’t really defined what the problem is very well and you’ve jumped into trying to build a database to represent something you haven’t defined clearly.

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

Sidebar

Related Questions

I've got into this habit of always using unsigned integers where possible in my
This seems like perhaps a naive question, but I got into a discussion with
Has anyone got to some good code to zoom into an image using javascript?
We've got some problems with an external company trying in integrate into a WCF
I've got an SQL query that looks like this: INSERT INTO DB..incident ( incident_number
I got into a bit of a debate yesterday with my boss about the
I got into another philosophical question regarding Java. It's about the following scenario: You
I got into the habit of coding my error handling this way: NSError* error
Alright well I recently got into normalizing my database for this little side project
I'm trying to duplicate a mailer I got into my gmail by taking a

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.