나는 이제 직원이 많은 비용 센터에게 비용 센터 소유자로을 가질 수 있습니다 내 레일 응용 프로그램has_many와 belongs_to 관계는
Class Employee
belongs_to :cost_center
End
Class CostCenter
has_many :employees
End
의 두 가지 모델이있다. 이 연관성을 레일에 어떻게 정의합니까?
나는 이제 직원이 많은 비용 센터에게 비용 센터 소유자로을 가질 수 있습니다 내 레일 응용 프로그램has_many와 belongs_to 관계는
Class Employee
belongs_to :cost_center
End
Class CostCenter
has_many :employees
End
의 두 가지 모델이있다. 이 연관성을 레일에 어떻게 정의합니까?
올바른 열이 있어야하지만 그렇지 않으면 쉽습니다.
class Employee
has_many :owned_cost_centers, :class_name => "CostCenter", :foreign_key => :owner_id
belongs_to :cost_center
end
class CostCenter
belongs_to :owner, :class_name => "Employee", :foreign_key => :owner_id
has_many :employees
end
완전성을 위해 모든 연결에 :inverse_of
을 추가해야합니다.
순환 참조가 필요하지 않습니다. 직원이 코스트 센터에 속한 경우 소유자는 코스트 센터에도 속해야합니다.
소유권과 고용 상태를 구별해야하는 경우 직원이 소유자와 다른 엔터티이기 때문에 두 가지 모델을 만드는 것이 좋습니다.
class Owner
belongs_to :cost_center
end
class CostCenter
has_many employees
has_one owner
end