Join tables, must they be created manually?

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 :slight_smile:

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. [email protected]

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?

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. [email protected]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs