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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:35:43+00:00 2026-06-05T06:35:43+00:00

I am trying to implement a many to many relation using a model in

  • 0

I am trying to implement a many to many relation using a model in rails 3 for my demo app.
It all worked fine untill I tried to add a model that will hold a bit more data on the relation.
I have a recipe, ingredient, Ingredient_Recipe models

File:ingredient_recipe.rb

class IngredientRecipe < ActiveRecord::Base
  attr_accessible :created_at, :ingredient_id, :order, :recipe_id
  belongs_to :recipes
  belongs_to :ingredients
end

File:ingredient.rb

class Ingredient < ActiveRecord::Base
 has_many :ingredientRecipe
 has_many :recipes, :through => :ingredientRecipe
 ...

File:recipes.rb

 class Recipe < ActiveRecord::Base
  has_many :ingredientRecipe
  has_many :ingredients, :through => :ingredientRecipe
  ...

in the ui

 <td >
  <% @recipe.ingredients.each do |ingredient| %>
   ingredient.name
  <% end %>
 </td >

table

 ingredient_id, recipe_id, order, created_at, updated_at

Now, this doesnt work so well…

oh well , and a good resource for implementing many to many would be very apprecieated

  • 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-06-05T06:35:45+00:00Added an answer on June 5, 2026 at 6:35 am

    I see a few errors in the model code. It is hard to say exactly what those might do.

    Your recipes model should look like this:

    has_many :ingredient_recipes
    has_many :ingredients, :through => :ingredient_recipes
    

    Your ingredient model should look like this

    has_many :ingredient_recipes
    has_many :recipes, :through => :ingredient_recipes 
    

    Associations should be underscored and lower-cased, has_many relationships should be pluralized.

    You said Ingredient_Recipe contains, the content is good but it should be named ingredient_recipe.rb and the class name should be IngredientRecipe unsure if that was just my misunderstanding.

    The first error you are experiencing said undefined method recipe_ingredient, which would make sense, the association name is ingredient_recipes, the through parameter takes the exact name of the association.

    the second issue is hard to say, but I would make the above modifications first and see if that improves the situation.

    the third item, I would say no. Follow the railscast’s instructions.

    The railscast in question is a good resource, I have used that specific one before.

    EDIT, just noticed the belongs_to is incorrect as well.

    within the IncredientRecipe class

    belongs_to :recipe
    belongs_to :ingredient
    

    a belongs_to association should be singular.

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

Sidebar

Related Questions

I am trying to implement a one-to-many relationship using NHibernate 2.1.2 but keep getting
I'm trying to implement a Rails3 nested form with has-many through association. My model
I'm trying to implement internationalization in JSF 2. I've tried many solutions, but I'm
I've been trying to implement trapezoid rule for double integral. I've tried many approaches,
I'm trying implement a way to recursively template using jsRender. The issue is, my
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app
am trying to implement a multiuser communication app. I want to identify a user's
I'm trying to implement a has_many :through join in Rails 3 (with Formtastic) and
Ok well I'm trying implement something similar to the 'undo' function in many image
I've been trying to implement POCO over WCF (using Entity Framework 4.0) - just

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.