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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:48:23+00:00 2026-05-13T11:48:23+00:00

While I’m not a complete Ruby/Rails newb, I’m still pretty green and I’m trying

  • 0

While I’m not a complete Ruby/Rails newb, I’m still pretty green and I’m trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of “recipes” for cooking.

A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in the database of all ingredients. That suggests two simple models:

class Ingredient < ActiveRecord::Base
  # ingredient name, 
end

class Recipe < ActiveRecord::Base
  # recipe name, etc.
end

If we just wanted to associate Recipes with Ingredients, that’s as simpling as adding the appropriate belongs_to and has_many.

But what if we want to associate additional information with that relationship? Each Recipe has one or more Ingredients, but we want to indicate the quantity of the Ingredient.

What is the Rails way to model that? Is it something along the lines of a has_many through?

class Ingredient < ActiveRecord::Base
  # ingredient name
  belongs_to :recipe_ingredient
end

class RecipeIngredient < ActiveRecord::Base
  has_one :ingredient
  has_one :recipe
  # quantity
end

class Recipe < ActiveRecord::Base
  has_many :recipe_ingredients
  has_many :ingredients, :through => :recipe_ingredients
end
  • 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-13T11:48:23+00:00Added an answer on May 13, 2026 at 11:48 am

    Recipes and Ingredients have a has and belongs to many relationship, but you want to store additional information for link.

    Essentially what you are looking for is a rich join model. But, a has_and_belongs_to_many relationship is not flexible enough to store the additional information you require. Instead you will need to use a has_many :through relatinship.

    This is how I would set it up.

    recipes columns: instructions

    class Recipe < ActiveRecord::Base
      has_many :recipe_ingredients
      has_many :ingredients, :through => :recipe_ingredients
    end
    

    recipe_ingredients columns: recipe_id, ingredient_id, quantity

    class RecipeIngredients < ActiveRecord::Base
      belongs_to :recipe
      belongs_to :ingredient
    end
    

    ingredient columns: name

    class Ingredient < ActiveRecord::Base
      has_many :recipe_ingredients
      has_many :recipes, :through => :recipe_ingredients
    end
    

    This will provide a basic representation of what you’re looking to do. You may want to add a validation to RecipeIngredients to ensure that each ingredient is listed once per recipe, and a callback to fold duplicates into one entry.

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

Sidebar

Related Questions

While I search this one out on my own, may be some one could
While reading from a site a read that you can not make a global
While trying problem 41 from the Euler Project, I ran across what seems to
While creating a simple client for a REST service which I have stubbed out,
While porting some code from Delphi 7 to Delphi 2010 I was rewriting my
while implementing hhtps, is there a way not to show the message Do you
While developing with Firebug I keep getting this error. pages[x].css(z-index,x) is not a function
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),
While going through university and from following the development of SO, I've heard a
While I've seen rare cases where private inheritance was needed, I've never encountered 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.