# How do I have two habtm between same two tables

**URL:** <https://www.ruby-forum.com/t/how-do-i-have-two-habtm-between-same-two-tables/77779>\
**Category:** Rails\
**Created:** [November 30, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/how-do-i-have-two-habtm-between-same-two-tables/77779 "2006-11-30T14:55:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [November 30, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/how-do-i-have-two-habtm-between-same-two-tables/77779/1 "2006-11-30T14:55:57Z")

</div>

I need two many-to-many models between the same two tables.

The two tables are documents and authors. I model documents and  
authors using habtm. I also need to model the records of the authors  
table as corresponding\_authors having a many-to-many relationship  
with documents. Do use habtm for the second model? Or another  
technique?

Jose

…  
Jose Hales-Garcia  
UCLA Department of Statistics  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [November 30, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/how-do-i-have-two-habtm-between-same-two-tables/77779/2 "2006-11-30T23:13:39Z")

</div>

Hello Jose,  
I’m not sure I quite understand the question, but I’ll attempt to  
help.  
The problem is, you have two models, Authors and Documents, and you have  
two  
many-to-many between them. Suppose one of these relationships is  
authorship  
(by that, I mean a particular author contributed to the writing of a  
particular document). Suppose the other relationship is …, I don’t  
know,  
mentions (by that I mean, a particular author is mentioned in a  
document).  
If that is the situation, I would create two models in addition to the  
Author and Document models: Authorship and Mention.

class Authorship \< ActiveRecord::Base  
belongs\_to :author # this author helped write …  
belongs\_to :document # this document  
end

class Mention \< ActiveRecord::Base  
belongs\_to :authors # this author is mentioned in …  
belongs\_to :documents # this document  
end

class Author \< ActiveRecord::Base  
has\_many :authorships  
has\_many :authored\_documents,  
:through =\> :authorships,  
:class\_name =\> ‘Document’,  
:foreign\_key =\> ‘document\_id’

has\_many :mentions  
has\_many :mentioned\_documents,  
:through =\> :mentions,  
:class\_mane =\> ‘Document’,  
:foreign\_key =\> ‘document\_id’  
end

class Document \< ActiveRecord::Base  
has\_many :authorships  
has\_many :authors,  
:through =\> :authorships,  
:class\_name =\> ‘Author’,  
:foreign\_key =\> ‘author\_id’

has\_many :mentions  
has\_many :mentioned\_authors,  
:through =\> :mentions,  
:class\_mane =\> ‘Author’,  
:foreign\_key =\> ‘author\_id’  
end

The names of my methods and variables are a little odd, but hopefully  
the  
concept is clear. You might want to watch DHH’s railsconf keynot on  
CRUD  
[1]. If I did not answer the right question (likely), please let me  
know,  
and I’ll try again.

[1]  
[http://blog.scribestudio.com/articles/2006/07/09/david-heinemeier-hansson-railsconf-2006-keynote-address](http://blog.scribestudio.com/articles/2006/07/09/david-heinemeier-hansson-railsconf-2006-keynote-address)
