# HABTM: two habtm's between the same two tables

**URL:** <https://www.ruby-forum.com/t/habtm-two-habtms-between-the-same-two-tables/80194>\
**Category:** Rails\
**Created:** [March 13, 2006, 5:00am UTC](https://www.ruby-forum.com/t/habtm-two-habtms-between-the-same-two-tables/80194 "2006-03-13T05:00:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 13, 2006, 5:00am UTC](https://www.ruby-forum.com/t/habtm-two-habtms-between-the-same-two-tables/80194/1 "2006-03-13T05:00:05Z")

</div>

Imagine I want to track people, and the clubs that they belong to.

table people with columns person\_id, person\_name  
table clubs with columns club\_id, club\_name

And I have the association table:

table clubs\_people with columns person\_id, club\_id

Now I know how to do this habtm between the two, in order to associate  
people with clubs that they belong to.

However my application also needs a secondary association just like  
this: imagine I want to track the clubs that a person has APPLIED to  
join, but is not yet a member.

So I have a second association table

table clubs\_people\_applied with columns person\_id, club\_id

In other words, I need TWO habtm’s between the same two tables.

Where this is failing I think is in the model definitions:

Class Person \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :clubs, :join\_table =\> clubs\_people  
has\_and\_belongs\_to\_many :clubs, :join\_table =\> clubs\_people\_applied  
…  
end

The rails api says that “WARNING: If youâ??re overwriting the table name  
of either class, the table\_name method MUST be declared underneath any  
has\_and\_belongs\_to\_many declaration in order to work.”

Is this the problem? I don’t understand what this means exactly, that  
is I don’t know how to declare the method.

Thanks!

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 13, 2006, 6:08am UTC](https://www.ruby-forum.com/t/habtm-two-habtms-between-the-same-two-tables/80194/2 "2006-03-13T06:08:59Z")

</div>

I’m not sure if either of these are the problems but

in the people table why not have the primary key be just “id” instead  
of “person\_id”? Same for the clubs table.

For the habtm statement, when you use :join\_table I think the table  
name has to be in quotation marks to make it a string

:join\_table =\> “clubs\_people”

In some cases, you might have to specify the foreign key in the join  
table.

Maybe one of these will fix the problem. I’ve used habtm specifying  
the join table without problems. It should work.

I’m looking forward to life without habtm once Rails 1.1 comes out  
with the has\_many :through business. (But thanks to the habtm author  
for what habtm enabled.)

- Peter

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 13, 2006, 6:51am UTC](https://www.ruby-forum.com/t/habtm-two-habtms-between-the-same-two-tables/80194/3 "2006-03-13T06:51:42Z")

</div>

Gaudi Mi wrote:

> Where this is failing I think is in the model definitions:
> 
> Class Person \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :clubs, :join\_table =\> clubs\_people  
> has\_and\_belongs\_to\_many :clubs, :join\_table =\> clubs\_people\_applied  
> …  
> end

You can’t use the same symbol (:clubs) as the object in more than one  
association. That’s what the Warning is trying to tell you.

You probably want the second habtm to read something like:

has\_and\_belongs\_to\_many :applied\_clubs, :join\_table =\>  
clubs\_people\_applied

-Brian

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 15, 2006, 1:53am UTC](https://www.ruby-forum.com/t/habtm-two-habtms-between-the-same-two-tables/80194/4 "2006-03-15T01:53:50Z")

</div>

Be aware that rails 1.0 has bug such that two habtm’s in one class just  
doesn’t work right. It’s been fixed in Edge. I can dredge up the exact  
bug/fix in svn if you’re interested.
