2

난 레일 3.0에있어 적절한 방법으로 belongs_to를 설정하는 것이 무엇인지 알아 내려고합니다. : 알고있는 관계를 통해 가능하지 않습니다. 내가 수행하여 해결할 수 있습니다 알고레일을 통해 belongs_to를 통해

class ParentCompany < ActiveRecord::Base 
    has_many :subsidiaries 
    has_many :employees, :through => :subsidiaries 
end 

class Subsidiary < ActiveRecord::Base 
    belongs_to :parent_company 
    has_many :employees 
end 

class Employee < ActiveRecord::Base 
    belongs_to :subsidiary 
    belongs_to :parent_company, :through :subsidiary # <-- I know this is invalid 
end 

: 예를 들면 다음과 같습니다이다 그러나

class Employee < ActiveRecord::Base 
    def parent_company 
    subsidiary.parent_company 
    end 
end 

, 나는 협회를 통해 위의 작업을 수행 할 수 있는지 알고 싶습니다.

답변

0

하면 연관

class Employee < ActiveRecord::Base 
    belongs_to :subsidiary 
    delegate :parent_company, to: :subsidiary 
end 
을 사용하지 않고이를 수행하는 delegate를 사용
관련 문제