铁轨通过同一型号的多个别名has_many

发布于 2025-02-05 11:27:01 字数 1524 浏览 3 评论 0原文

我有3种型号。档案 - > ArchiveJoin< - 联系人

存档Join是一张多态性联接表。我希望能够通过加入表与档案馆联系并联系,并能够使用别名查询它们。

例如,可以将联系人引用为作者,合着者,出版商等。

我的解决方案是在存档表上,我将其引用这样的关联。

    has_many :author, :through => :archive_joins, :source => :archiveable, :source_type => 'Contact'
    has_many :co_author, :through => :archive_joins, :source => :archiveable, :source_type => 'Contact'
    has_many :publisher, :through => :archive_joins, :source => :archiveable, :source_type => 'Contact'

问题是,当我称其中任何一个时,我会得到相同的联系人,而不是在那些别名名称之间区分。

irb(main):082:0> Archive.first.author
=> #<ActiveRecord::Associations::CollectionProxy [#<Contact id: 1, created_at: "2022-06-06 14:26:35", updated_at: "2022-06-06 14:26:35">, #<Contact id: 2, created_at: "2022-06-06 15:36:24", updated_at: "2022-06-06 15:36:24">]>

irb(main):083:0> Archive.first.co_author
=> #<ActiveRecord::Associations::CollectionProxy [#<Contact id: 1, created_at: "2022-06-06 14:26:35", updated_at: "2022-06-06 14:26:35">, #<Contact id: 2, created_at: "2022-06-06 15:36:24", updated_at: "2022-06-06 15:36:24">]>

irb(main):084:0> Archive.first.publisher
=> #<ActiveRecord::Associations::CollectionProxy [#<Contact id: 1, created_at: "2022-06-06 14:26:35", updated_at: "2022-06-06 14:26:35">, #<Contact id: 2, created_at: "2022-06-06 15:36:24", updated_at: "2022-06-06 15:36:24">]>

有什么方法可以通过导轨解决,还是我必须创建自定义方法来区分?任何帮助将不胜感激。

I have 3 Models. Archive -> ArchiveJoin <- Contacts

ArchiveJoin is a polymorphic join table. I want to be able to associate Archive and contacts through the join table and be able to query them using an alias.

For example a contact can be referenced as Author, Co-author, publisher etc.

My solution for this is on the archive table I reference the association like this.

    has_many :author, :through => :archive_joins, :source => :archiveable, :source_type => 'Contact'
    has_many :co_author, :through => :archive_joins, :source => :archiveable, :source_type => 'Contact'
    has_many :publisher, :through => :archive_joins, :source => :archiveable, :source_type => 'Contact'

The problem is that when I call any one of these I get the same contact rather then differentiating between there aliased names.

irb(main):082:0> Archive.first.author
=> #<ActiveRecord::Associations::CollectionProxy [#<Contact id: 1, created_at: "2022-06-06 14:26:35", updated_at: "2022-06-06 14:26:35">, #<Contact id: 2, created_at: "2022-06-06 15:36:24", updated_at: "2022-06-06 15:36:24">]>

irb(main):083:0> Archive.first.co_author
=> #<ActiveRecord::Associations::CollectionProxy [#<Contact id: 1, created_at: "2022-06-06 14:26:35", updated_at: "2022-06-06 14:26:35">, #<Contact id: 2, created_at: "2022-06-06 15:36:24", updated_at: "2022-06-06 15:36:24">]>

irb(main):084:0> Archive.first.publisher
=> #<ActiveRecord::Associations::CollectionProxy [#<Contact id: 1, created_at: "2022-06-06 14:26:35", updated_at: "2022-06-06 14:26:35">, #<Contact id: 2, created_at: "2022-06-06 15:36:24", updated_at: "2022-06-06 15:36:24">]>

Is there any way to resolve through rails or would I have to create custom methods to differentiate? Any help would be appreciated.

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

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

发布评论

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

评论(1

煮茶煮酒煮时光 2025-02-12 11:27:01

是的,它可以使用Rails完成,但是我们需要在ArchiveJoin表上进行额外的列来指示它具有哪种关联。 (联系可以保持不变,以便可以既可以是作者,合着者或出版商,而无需重复)。

假设我们称额外的列为“ Archive_join_type”,我们可以做到这一点:

class Contact < ApplicationRecord
  has_many :author_archive_joins, -> { where(archive_join_type: :author) }, class_name: "ArchiveJoin"
  has_many :coauthor_archive_joins, -> { where(archive_join_type: :coauthor) }, class_name: "ArchiveJoin"
  has_many :publisher_archive_joins, -> { where(archive_join_type: :publisher) }, class_name: "ArchiveJoin"

  has_many :author, through: :author_archive_joins, source: :archiveable, source_type: 'Contact'
  has_many :co_author, through: :coauthor_archive_joins, source: :archiveable, source_type: 'Contact'
  has_many :publisher, :through: :publisher_archive_joins, source: :archiveable, source_type: 'Contact'
end

Yes it can be done with Rails, but we need an extra column on ArchiveJoin table to indicate which kind of association it has. (Contact can stay the same so that one can be both Author, Co-author or Publisher without duplications).

Assume that we call the extra column "archive_join_type", we can do this:

class Contact < ApplicationRecord
  has_many :author_archive_joins, -> { where(archive_join_type: :author) }, class_name: "ArchiveJoin"
  has_many :coauthor_archive_joins, -> { where(archive_join_type: :coauthor) }, class_name: "ArchiveJoin"
  has_many :publisher_archive_joins, -> { where(archive_join_type: :publisher) }, class_name: "ArchiveJoin"

  has_many :author, through: :author_archive_joins, source: :archiveable, source_type: 'Contact'
  has_many :co_author, through: :coauthor_archive_joins, source: :archiveable, source_type: 'Contact'
  has_many :publisher, :through: :publisher_archive_joins, source: :archiveable, source_type: 'Contact'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文