2017-03-15 3 views
0

나는 includes을 사용하여 모든 필요한 연관을 열심히로드하는 scope 방법을 사용합니다. 하지만 includes에 사용 된 연관성을 어떻게 줄일 수 있을지 모르겠습니다. 여기 포함에서 협회를 어떻게 축축하게합니까?

모델에 정의 된 scope입니다 :

scope :with_associates, (lambda do 
    includes(user: :contact, 
      appointments: [:customer, templates: :page], 
      bundles: appointments: [:customer, templates: :page]]) 
    end) 

방법이 범위를 건조?

답변

2

나는 결코 그 드라이를 시도하지 않았다. 어쨌든, 내가 해 볼게.

def appointments_for_includes 
    Hash[appointments: [:customer, templates: :page]] 
end 

def associations_for_includes 
    Hash[user: :contact, bundles: appointments_for_includes].merge(appointments_for_includes) 
end 
0

DRYing에서 생각하는 것처럼 덜 명확하게 보입니다. appointments: [:customer, templates: :page]의 반복을 위해서, 이것의 건조 비용은 원래의 선명도보다 더 큽니다

+0

네,하지만 다른 사람들을 위해'약속 : [: 고객, 템플릿 : : 페이지] '를 사용하고 싶다면 범위라면 건조시켜야합니다. –

+0

예 - 맞는 말이지 만 귀하의 예에서는 주어지지 않았습니다. – ReggieB

관련 문제