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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:10:24+00:00 2026-05-18T06:10:24+00:00

I wanted to build a basic cookbook. With a Recipes habtm Ingredients Relation. My

  • 0

I wanted to build a basic cookbook. With a Recipes habtm Ingredients Relation.

My first attempt was like this.

class Recipe < ActiveRecord::Base
  # title, description
  has_many :recipe_ingredients
end

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

class RecipeIngredient < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient
  attr_accessible :amount
end

And created the Relation by Hand

RecipeIngredient.create(:recipe_id => 1, :ingredient_id => 2, :amount => 100)

recipe.recipe_ingredients.amout
recipe.recipe_ingredients.ingredient.unit
recipe.recipe_ingredients.ingredient.name

This feels ugly. But I don’t know any other solution.

  • 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-18T06:10:24+00:00Added an answer on May 18, 2026 at 6:10 am

    Looks fine to me, as a schema/approach. I think it might just feel ugly because your choice of class name leads you to type “recipe.recipe_ingredients.ingredient” a lot. To me, an ‘ingredient’ is the food/liquid/whatever AS IT IS USED IN THE RECIPE, so the join table should be called ‘ingredients’. Each ingredient has a quantity and links to a ‘product’ or an ‘item’ or something similar.

    I would rename it thus:

    Recipe
      has_many :ingredients
      has_many :items, :through => :ingredients
    
    Ingredient
      #fields - recipe_id, item_id, quantity(string)
      belongs_to :recipe
      belongs_to :item
    
    Item
      has_many :ingredients
      has_many :recipes, :through => :ingredients
    

    Now on your view page you can say

    <h2><%= recipe.name %></h2>
    <dl>
      <% @recipe.ingredients.each do |ingredient| %>
        <dt><%= ingredient.item.name %></dt>
        <dd><%= ingredient.quantity %></dd>
      <% end %>
    </dl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
I have this class that I wanted to build using TDD, but I failed.
First I wanted to build the DBD::mysql package. That kept failing because whatever make
I managed to build a basic IFrame application, using the Graph API. I wanted
I am trying to build a basic plugin system like the kind you often
Here’s a really really super-basic question. Say I wanted to build – today –
i wanted to build a search engine like google where if i enter a
I wanted to build a matrix representation parsimony with R. I found this phylogenetic
I wanted to build a particle effect with kinect like the one james alliban
I am newbie at PHP. I wanted to build a small project of room

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.