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 troubleshooting a misbehavior in Spree where the product list was not paginating and
While listening through Stanford's Programming Abstractions course, I come across some piece of code
While trying to call an EJB made using NETbeans (using jdk1.7) from a client
While parsing the XML with SAX parser in JAVA, I am not able to
while working with linq query i'm receiving error System.NullReferenceException: Object reference not set to
While trying to install a program from source I found that it installed libraries
while setting up a simple Rails app, I met an issue i never had
While working with some jQuery ajax, I've run into this problem with Chrome and
while i'm using push notification in iphone with local server(used php), not able to
while working i messed HTML because i did not knew that <p> tag within

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.