How do I have two habtm between same two tables

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

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