# Has\_many etc

**URL:** <https://www.ruby-forum.com/t/has-many-etc/84279>\
**Category:** Rails\
**Created:** [February 2, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/has-many-etc/84279 "2007-02-02T21:44:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 2, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/has-many-etc/84279/1 "2007-02-02T21:44:21Z")

</div>

I’ve got myself quite mixed up on has\_many and related issues.

Suppose I have a lot of Recipes, each of which is to have many  
Ingredients.

I think I am meant to do e.g. models/recipe.rb

class Recipe \< ActiveRecord::Base  
has\_many :ingredients  
end

along with models/ingredient.rb

class Ingredient \< ActiveRecord::Base  
belongs\_to :recipe  
end

Is that right, with the has\_many and belongs\_to, with the plurality  
and use of upper and lower case?

Now I come to my real confusion. I half-think I should create a model  
“Recipes\_Ingredients”, but I’m not sure on that, so I’ve not done it.

What I _have_ done is to create a table in one of my migrations, viz

class RecipesIngredients \< ActiveRecord::Migration  
def self.up  
create\_table :recipes\_ingredients, :id =\> false do |t|  
t.column :ingredient\_id, :integer  
t.column :recipe\_id, :integer  
end  
add\_index :recipes\_ingredients, [:ingredient\_id, :recipe\_id]  
add\_index :recipes\_ingredients, :recipe\_id  
end  
def self.down  
drop\_table :recipes\_ingredients  
end  
end

but is this enough? Am I supposed to use “execute” lines to make up  
foreign keys, or will this do what I need?

I think that what I’ve done is insufficient, because no method  
recipe.ingredient  
seems to have been created. (Hm… is there a way to make the system  
describe the methods it’s creating, during migration and otherwise?)

PS. I note that the Roll-With Revisited tutorial [http://www.onlamp.com/](http://www.onlamp.com/)  
pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html?  
page=2 illustrates the use of MYSQL code

constraint fk\_recipes\_categories foreign key (category\_id)  
references categories(id), primary key(id)

but I’m hoping to avoid hand injection of MySQL. (If I wanted to work  
with MySQL directly, I’d be back with PHP!)

Well, I hope I’ve been clear. As readers may guess, I’m not really  
working on recipes and ingredients, so I hope I’ve done tthe  
conversion to these more evocative names correctly enough that my  
point is clear.

PS. I’m using the new RoR, with MySQL.

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 2, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/has-many-etc/84279/2 "2007-02-02T23:20:33Z")

</div>

On 2-Feb-07, at 3:44 PM, dankelley wrote:

> end
> 
> along with models/ingredient.rb
> 
> class Ingredient \< ActiveRecord::Base  
> belongs\_to :recipe  
> end
> 
> Is that right, with the has\_many and belongs\_to, with the plurality  
> and use of upper and lower case?

this article should get you going Dan - funny enough it’s on subject  
too!

[http://blog.hasmanythrough.com/2006/3/28/the-other-ways-through](http://blog.hasmanythrough.com/2006/3/28/the-other-ways-through)

Cheers,  
Jodi  
General Partner  
The nNovation Group inc.  
www.nnovation.ca/blog

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [February 3, 2007, 2:13am UTC](https://www.ruby-forum.com/t/has-many-etc/84279/3 "2007-02-03T02:13:46Z")

</div>

(Answer to own question.)

I realized that models/ingredients.rb needs

has\_and\_belongs\_to\_many :recipes

and that models/recipes.rb needs

has\_and\_belongs\_to\_many :ingredients

to get the desired effect.

PS to Jodi – that’s a very informative site you pointed out; thanks  
very much.
