铁轨通过同一型号的多个别名has_many
我有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它可以使用Rails完成,但是我们需要在ArchiveJoin表上进行额外的列来指示它具有哪种关联。 (联系可以保持不变,以便可以既可以是作者,合着者或出版商,而无需重复)。
假设我们称额外的列为“ Archive_join_type”,我们可以做到这一点:
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: