Join table should be "baz" or "foos_bars"?

http://wiki.rubyonrails.org/rails/pages/ThroughAssociations suggests,
essentially, using “baz” while I believe that I’ve seen sources
suggesting “foos_bars” and, I believe, “foo_bars”. In particular, on
this list, joining tables “foos” and “bars” with a many-to-many,
“foos_bars” was recommended for the join table.

Why? Does it matter? What’s correct?

thank you,

Thufir

Check out this ActiveRecord tutorial:

http://railsenvy.com/tags/activerecord%20tutorial%20video

Skip to about 2/3 of the way in and there is a section on many-to-many
relationships that explains it quite well.

I always used foos_bars as a join table for a habtm association.
I don’t get the ‘baz’ but i think it references when you have a many
to many relationship through something other than a join table?

with habtm, you have to use foos_bars. With has many :through, the
join table in a model in its own right and so you can call it anything
you want. When possible you should strive to find a ‘proper’ name for
the join model. For example if your app was a forum type application,
where users could subscribe to topics then a good name for the join
model between users and topics would be ‘subscription’

Fred

On Dec 14, 12:15 pm, Charles [email protected] wrote:

I always used foos_bars as a join table for a habtm association.
I don’t get the ‘baz’ but i think it references when you have a many
to many relationship through something other than a join table?
[…]

I don’t have it in front of me, but in the “rails recipes” book, one
of the recipes is for a habtm relationship. They have three tables,
foos, bars, and baz. They don’t use “foos_bars” to my recollection.
That is, the name for the join table is unrelated to the name for
either table. However, that’s based on my recollection that it’s a
habtm relationship, but I think so.

-Thufir

This is my take on it:

OLD WAY - has_and_belongs_to_many :my_model

This is an out of date technique to set up the many-to-many table. A 

major limitation to this method is that you cannot have any more fields
in the table then the 2 joining fields, and the join table does not use
a primary field key called ‘id’. You can’t sort joined fields very
easily with this method.

This technique is very easy to set up. The table name is the plural 

names of the 2 joining tables, in alphabetical order, separated by an
underscore. Table C and B would be BS_CS. The only 2 fields in the join
table would be b_id and c_id.

NEW WAY - has_many :through

This technique allows the join table to have as many fields as you 

want. You need a model, for the joining, to make this work. You can just
generate a model. But if you need more fields, you can scaffold. The
join table also gets the primary key field (’id’) that is automatically
generated in most cases.

The joining table can be named what ever you want. The current trend 

is to use a table name that just has one word, and usually ends in
-ship, -tion, or -ment. There can be endless hours of debate over the
name of the joining table.

You will need to have the 2 joining table’s foreign key fields in 

this table, like the fashion ‘_id’, that are not pluralized.

If you wanted to join table A and table B, an example of the joining 

table could be:

TABLES

    A

        id (integer, primary key)
        name (string)

    B

        id (integer, primary key)
        name (string)

    AB (many table for joining tables A and B)

        id (integer, primary key)
        a_id (integer)
        b_id (integer)
        … (more fields)

The MODELS

    A

        has_many :ABs
        has_many :Bs :through => :ABs

    AB

        belongs_to :A
        belongs_to :B

    B

        has_many :ABs
        has_many :As :through => :ABs

SOME OPTIONS

    has_many :mytable,     :foreign_key => 'myfield_id',
                           :class_name => 'myclassname',
                           :dependent => :destroy

On 12/14/07, gemblon (t.b.) [email protected] wrote:

This is my take on it:

OLD WAY - has_and_belongs_to_many :my_model

This is an out of date technique to set up the many-to-many table. A

major limitation to this method is that you cannot have any more fields
in the table then the 2 joining fields, and the join table does not use
a primary field key called ‘id’. You can’t sort joined fields very
easily with this method.

This makes it sound like HABTM is obsolete. It isn’t if you don’t
need to have attributes on the association, I think it serves
perfectly way.

This technique is very easy to set up. The table name is the plural

names of the 2 joining tables, in alphabetical order, separated by an
underscore. Table C and B would be BS_CS. The only 2 fields in the join
table would be b_id and c_id.

And if the too records were foo and bar, the join table would be
bars_foos, not foos_bars as has been mistakenly said at least once in
this thread.

On the other hand, the link in the original post was to a description
of :has_many :through and I think that the OP was confused between
examples of HABTM and HM T.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

I don’t have it in front of me, but in the “rails recipes” book, one
of the recipes is for a habtm relationship. They have three tables,
foos, bars, and baz. They don’t use “foos_bars” to my recollection.
That is, the name for the join table is unrelated to the name for
either table. However, that’s based on my recollection that it’s a
habtm relationship, but I think so.

from the api documentation:
“Unless the join table is explicitly specified as an option, it is
guessed using the lexical order of the class names. So a join between
Developer and Project will give the default join table name of
developers_project”

On Tue, 18 Dec 2007 08:59:33 -0500, Rick DeNatale wrote:

And if the too records were foo and bar, the join table would be
bars_foos, not foos_bars as has been mistakenly said at least once in
this thread.

What’s the distinction between foos_bars and bars_foos please? A
many-to-
many isn’t “directional”, is it?

thanks,

Thufir

On 19 Dec 2007, at 08:54, Thufir wrote:

many isn’t “directional”, is it?

No, but you’ve got to pick something and by default hatbm would pick
bars_foos (My mistake for saying foos_bars earlier - Time to brush up
on my alphabet :slight_smile: )

Fred

On Tue, 18 Dec 2007 08:59:33 -0500, Rick DeNatale wrote:

On the other hand, the link in the original post was to a description of
:has_many :through and I think that the OP was confused between
examples of HABTM and HM T.

Yeh, a bit. Ooops :slight_smile:

-Thufir

On Wed, 19 Dec 2007 09:04:32 +0000, Frederick C. wrote:

No, but you’ve got to pick something and by default hatbm would pick
bars_foos (My mistake for saying foos_bars earlier - Time to brush up on
my alphabet )

Oh, because that’s alphabetically sorted?

-Thufir

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