双向自我参照关联
以 Ryan Bates 的 asciicast 为例: http://asciicasts.com/episodes/163-self-referential-association
的两个关联结束。
- 他以 User :friends
- :inverse_friends
鉴于用户不会关心谁煽动了友谊,您将需要一个简单的 User 关联
- :friends
:由两种关系组成的 。即由用户发起的关系和由用户的朋友发起的关系。
那么如何才能实现这种双向自我参照关联呢?
更新 - Josh Susser 在这里发表了一篇关于此的文章: http://blog.hasmanythrough.com/2006/4/21/ self-referential-through
然而,它仍然讨论 has_many :sources 和 has_many :sinks ,而实际上应该有一个包含源和接收器的 has_many :nodes 。
Taking Ryan Bates' asciicast as an example:
http://asciicasts.com/episodes/163-self-referential-association
He ends with two associations of User
- :friends
- :inverse_friends
Given that a user would not care who instigated the friendship, you would want a User association that was simply
- :friends
that consisted of both relationships. i.e Relationships instigated by the user and relationships instigated by the user's friend.
So how can you achieve this bidirectional self-referential association?
UPDATE - Josh Susser has a post about this here:
http://blog.hasmanythrough.com/2006/4/21/self-referential-through
However, it still talks about has_many :sources and has_many :sinks when really there should be a has_many :nodes that includes both the sources and the sinks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这对你有用吗?
这是给定的数据库表模式
see if this works for you?
this is given a database table schema of