How do I do multiple has_and_belongs_to_many associations between the same two classes?
Posted
by Ermin
on Stack Overflow
See other posts from Stack Overflow
or by Ermin
Published on 2010-05-20T17:33:28Z
Indexed on
2010/05/20
23:50 UTC
Read the original article
Hit count: 247
ruby-on-rails
|habtm
I have the following setup:
class Publication < ActiveRecord::Base
has_and_belongs_to_many :authors, :class_name=>'Person', :join_table => 'authors_publications'
has_and_belongs_to_many :editors, :class_name=>'Person', :join_table => 'editors_publications'
end
class Person < ActiveRecord::Base
has_and_belongs_to_many :publications
end
With this setup I can do stuff like Publication.first.authors
. But if I want to list all publications in which a person is involved Person.first.publications
, an error about a missing join table people_publications
it thrown. How could I fix that?
Should I maybe switch to separate models for authors and editors? It would however introduce some redundancy to the database, since a person can be an author of one publication and an editor of another.
© Stack Overflow or respective owner