双向自我参照关联

发布于 2024-09-03 02:12:33 字数 732 浏览 8 评论 0原文

以 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 技术交流群。

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

发布评论

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

评论(1

爱格式化 2024-09-10 02:12:33

看看这对你有用吗?

class User < ActiveRecord::Base
  has_many :friendships, :foreign_key => "person_id", :class_name => "Friendship"
  has_many :friends, :through => :friendships

  def befriend(user)
    # TODO: put in check that association does not exist
    self.friends << user
    user.friends << self
  end
end

class Friendship < ActiveRecord::Base
  belongs_to :person, :foreign_key => "person_id", :class_name => "User"
  belongs_to :friend, :foreign_key => "friend_id", :class_name => "User"  
end

# Usage
jack = User.find_by_first_name("Jack")
jill = User.find_by_first_name("Jill")

jack.befriend(jill)

jack.friends.each do |friend|
  puts friend.first_name
end
# => Jill

jill.friends.each do |friend|
  puts friend.first_name
end
# => Jack

这是给定的数据库表模式

users
  - id
  - first_name
  - etc...

friendships
  - id
  - person_id
  - friend_id

see if this works for you?

class User < ActiveRecord::Base
  has_many :friendships, :foreign_key => "person_id", :class_name => "Friendship"
  has_many :friends, :through => :friendships

  def befriend(user)
    # TODO: put in check that association does not exist
    self.friends << user
    user.friends << self
  end
end

class Friendship < ActiveRecord::Base
  belongs_to :person, :foreign_key => "person_id", :class_name => "User"
  belongs_to :friend, :foreign_key => "friend_id", :class_name => "User"  
end

# Usage
jack = User.find_by_first_name("Jack")
jill = User.find_by_first_name("Jill")

jack.befriend(jill)

jack.friends.each do |friend|
  puts friend.first_name
end
# => Jill

jill.friends.each do |friend|
  puts friend.first_name
end
# => Jack

this is given a database table schema of

users
  - id
  - first_name
  - etc...

friendships
  - id
  - person_id
  - friend_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文