为了在活动记录中建立自引用关系,我遵循了 rails-cast #163 http://railscasts.com/episodes/163-self-referential-association中显示的自引用模型,但我不喜欢这样模型,因为我必须写friendship和inverse_friendships,我不能只做类似的事情:
在我的人.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
在我的友谊中.rb
class Friendship
belongs_to :person
belongs_to :friend, :class_name => "Person"
end