2013-02-13 3 views
0

활성 레코드에서 자체 참조 관계를 만들기 위해 레일스 캐스트 # 163 http://railscasts.com/episodes/163-self-referential-association에 표시된 자체 참조 모델을 따라 갔지만이 모델은 내가 우정을 작성해야하기 때문에이 모델을 좋아하지 않습니다 inverse_friendships는, 그냥 같은 것을 할 수 없었 : 내 person.rb자체 참조 관계 ActiveRecord

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 

에 내 friendship.rb에

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

답변

2

이름이 비슷한 연결을 사용할 수 없으므로이 이름은 모델 내에서 고유해야합니다. 그렇지 않은 경우 ActiveRecord는 코드에 @user.friendships이있을 때 어떤 friendships을 의미하는지 어떻게 알 수 있습니까?

+0

이 질문에 유용한 @apneadiving에서 대답하지 않습니까? http://stackoverflow.com/a/4727419/976775 – MrYoshiji

+0

하하하, 예를 들어 프로젝트를 혼란스럽게합니다. 수정합니다. +1 – OneChillDude