# Join tables, must they be created manually?

**URL:** <https://www.ruby-forum.com/t/join-tables-must-they-be-created-manually/104545>\
**Category:** Rails\
**Created:** [May 19, 2007, 5:16am UTC](https://www.ruby-forum.com/t/join-tables-must-they-be-created-manually/104545 "2007-05-19T05:16:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [May 19, 2007, 5:16am UTC](https://www.ruby-forum.com/t/join-tables-must-they-be-created-manually/104545/1 "2007-05-19T05:16:55Z")

</div>

I’m sure this is a silly question, but I’ve been reading through the  
agile rails book and no surprises about using join tables so far.

But I looked into the rails cookbook and it has this to say on the  
matter:

“The many-to-many relationship between recipes and tags also needs the  
help of Active Record declarations. We’ve added has\_and\_belongs\_to\_many  
:tags to recipes.rb and has\_and\_belongs\_to\_many :recipes to tags.rb.  
There’s no sign of the intermediate join table, recipes\_tags; this is by  
design. Active Record handles the complexities of maintaining  
many-to-many relationships and provides an intuitive interface for  
accessing them from within Rails.”

I also looked through the sitepoint rails book and there wasn’t even a  
mention of defining ‘join tables’ for use with has\_and\_belongs\_to\_many.

So now I’m confused, agile rails seems like a highly credible book but  
as far as I can tell it defines the manual creation of join tables as  
necessary, while the other two books either don’t mention them at all or  
give the impression they are maintained internally, or at least can be.

I realize I’m probably overlooking something very obvious, but I’ll  
accept my humiliation for an answer to my dilemma 🙂

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [May 19, 2007, 11:30am UTC](https://www.ruby-forum.com/t/join-tables-must-they-be-created-manually/104545/2 "2007-05-19T11:30:21Z")

</div>

All table must be created manually or (preferably) in a migration. By  
default (name conventings can be configured to some extent), habtm  
expects a table named recipes\_tags (alphabetical order) with a  
recipe\_id field and a tag\_id field AND NOTHING ELSE.

So if you are creating it in a migration, you have to make sure you  
don’t get an id field. It’s easy for that to happen since migrations  
add it silently by default. You will want to add :id =\> false when you  
call create\_table in the migration

On May 18, 9:16 pm, Michael C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [May 19, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/join-tables-must-they-be-created-manually/104545/3 "2007-05-19T17:30:56Z")

</div>

Interesting. I have not had the time to re-create the application in the  
Sitepoint ruby on rails book, so am I to assume there are no  
circumstances under which habtm could work without manually defining a  
join table, thus the application in the book would fail?

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [May 19, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/join-tables-must-they-be-created-manually/104545/4 "2007-05-19T22:42:27Z")

</div>

It’s hard to believe they would overlook something that obvious. They  
don’t define the join table where they define the other tables?

On May 19, 9:30 am, Michael C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [May 20, 2007, 4:10am UTC](https://www.ruby-forum.com/t/join-tables-must-they-be-created-manually/104545/5 "2007-05-20T04:10:51Z")

</div>

On second look the habtm relationship isn’t used in the demo  
application, it’s merely mentioned separately, although again, there is  
no mention of join tables. It is a ‘beginner’ book so I suppose it’s  
more of a “do further research” factor rather than “forgot to include  
something important” factor.

As for the Rails Cookbook suggesting there is “no sign of join table by  
design”, I’m assuming this probably means not that there isn’t a join  
table, but rather there doesn’t need to be a mention of it in the model.

Seems like nothing more than a bit of misunderstanding on my part,  
thanks.
