2012-11-21 2 views
1

직원이 관리자와 일부 종속자 (모두 직원 모델의 예가 될 수 있음)가있는 관계를 어떻게 정의 할 수 있습니까? 지금까지의 시도는 주어진 사람이 최대 한 명의 종속자를 갖는 결과를 낳습니다.레일 관계

has_one :manager, :class_name => Employee, :foreign_key => "manager" 
has_many :subordinates, :class_name => Employee, :foreign_key => "manager" 

나는이 하나의 명백한 같은 느낌,하지만 벽에 내 머리의 모든 두드리는 어려운하고있다.

+1

당신은'belongs_to'를 사용해야합니다. –

답변

5

이 하나를 시도

class Employee 

    belongs_to :manager,  :class_name => 'Employee', :inverse_of => :subordinates 
    has_many :subordinates, :class_name => 'Employee', :inverse_of => :manager 

end