2011-02-08 8 views
2

내가이일부 쿼리는 어떻게 제거합니까?

<% @courses.each do |course| %> 
    <tr> 
     <th>Users</th> 
    </tr> 
    <tr> 
    <td> 
    <span><%= course.not_competent %></span> <span><%= course.novice %></span></td> 
    </tr> 
<% end %> 

내 코스 모델에 나는이

def novice 
    self.courses_users.low_scores.collect{|score| score.user.username}.join(', ') 
end 

def not_competent 
    self.courses_users.really_low_scores.collect{|score| score.user.username}.join(', ') 
end 

문제가이 일을하고, 그래서 확인은 .... 쿼리의 수천을 실행이 접근하는 더 나은 방법이 그 DB를

+3

필자가 생각한 ': include'를 사용하십시오. – Zabba

답변

1

바로 @Zabba을 가지고하지 않습니다, @courses을 채우는 전화를 변경하고이 비슷한을 추가 할 처음 시도

PARAMS

또는 체인

.includes([{:courses_users => :low_scores}]) 

이 방식으로

(내가 low_scores를 가정 해 다른 모델의 협회입니다) 액티브 당신이 중첩 된 tables.in이 ascii_cast 몇 가지 예를 포함하여이 첫 번째 쿼리를 변경 .

유용 할 수 있기를 바랍니다.

관련 문제