我有以下設置:如何在相同的兩個類之間執行多個has_and_belongs_to_many關聯?
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
有了這個設置,我可以做的東西一樣Publication.first.authors
。但是,如果我想列出涉及某人的所有出版物Person.first.publications
,則會拋出關於丟失的連接表people_publications
的錯誤。我怎麼能解決這個問題?
我是否應該改用作者和編輯的獨立模型?然而,它會給數據庫帶來一些冗餘,因爲一個人可以是一個出版物的作者和另一個出版物的編輯。
has_and_belongs_to_many另一個關聯:authored_publications,:CLASS_NAME => 「出版」,:join_table =>:authors_publications – 2010-05-21 13:05:05
has_and_belongs_to_many:edited_publications,:CLASS_NAME => 「出版」 ,:join_table =>:editors_publications – 2010-05-21 13:06:19