如何在相同的两个类之间进行多个 has_and_belongs_to_many 关联?

发布于 2024-09-02 00:09:56 字数 633 浏览 7 评论 0原文

我有以下设置:

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 的错误。我该如何解决这个问题?

我是否应该为作者和编辑切换到单独的模型?然而,它会给数据库带来一些冗余,因为一个人可以是一份出版物的作者和另一份出版物的编辑。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-09-09 00:09:56

关联的另一端可能应该称为 authored_publicationsedited_publications 之类的名称,并带有一个额外的只读 publications 访问器,该访问器返回二。

否则,如果你尝试做类似的事情,你就会遇到棘手的情况,

person.publications << Publication.new

因为你永远不知道这个人是作者还是编辑。这并不是说不能通过稍微改变对象模型来解决这个问题。

您还可以在 ActiveRecord 中进行一些修改来更改 SQL 查询或更改关联的行为,但也许只是保持简单?

The other end of your associations should probably be called something like authored_publications and edited_publications with an extra read-only publications accessor that returns the union of the two.

Otherwise, you'll run in to sticky situations if you try to do stuff like

person.publications << Publication.new

because you'll never know whether the person was an author or an editor. Not that this couldn't be solved differently by slightly changing your object model.

There's also hacks you can do in ActiveRecord to change the SQL queries or change the behavior of the association, but maybe just keep it simple?

橙幽之幻 2024-09-09 00:09:56

我相信你应该在 person 模型上有另一个关联

class Person < ActiveRecord::Base 
  # I'm assuming you're using this names for your foreign keys
  has_and_belongs_to_many :author_publications, :foreign_key => :author_id
  has_and_belongs_to_many :editor_publications, :foreign_key => :editor_id
end 

I believe you should have another association on person model

class Person < ActiveRecord::Base 
  # I'm assuming you're using this names for your foreign keys
  has_and_belongs_to_many :author_publications, :foreign_key => :author_id
  has_and_belongs_to_many :editor_publications, :foreign_key => :editor_id
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文