Ruby on rails 自引用关系ActiveRecord

Ruby on rails 自引用关系ActiveRecord,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,为了在活动记录中建立自我参照关系,我遵循了rails cast#163中所示的自我参照模型,但我不喜欢这个模型,因为我随后必须编写Friends和Reverse\u Friends,我能不能这样做: 以我个人的名义.rb class Person has_many :friendships has_many :friendships, :foreign_key => "friend_id" has_many :friends, :through => :friendsh

为了在活动记录中建立自我参照关系,我遵循了rails cast#163中所示的自我参照模型,但我不喜欢这个模型,因为我随后必须编写FriendsReverse\u Friends,我能不能这样做:

以我个人的名义.rb

class Person 
  has_many :friendships
  has_many :friendships, :foreign_key => "friend_id"
  has_many :friends, :through => :friendships
  has_many :friends, :through => :friendships, :source => :person
end
还有我的友谊

class Friendship
  belongs_to :person
  belongs_to :friend, :class_name => "Person"
end

不,您不能有类似命名的关联,这些名称在模型中必须是唯一的。否则,当你的代码中有
@user.friendships
时,ActiveRecord如何知道你指的是哪种
友谊?

来自@apreading的回答对这个问题有用吗?哈哈哈,我把我的项目和这个例子搞混了。将编辑+1.