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 jose-oNoWckAxTcaFhjwBz98joA@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jimmy Kittiyachavalit
2006-Nov-30 22:11 UTC
Re: How do I have two habtm between same two tables
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
On 11/30/06, Jose Hales-Garcia
<jose-oNoWckAxTcaFhjwBz98joA@public.gmane.org>
wrote:>
>
> 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
> jose-oNoWckAxTcaFhjwBz98joA@public.gmane.org
>
>
>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---