현재 내 응용 프로그램에서 나는 다음과 같은 모델이 있습니다어떤 many_to_many 모델을 사용하여 데이터베이스 쿼리를 효율적으로 사용해야합니까?
- 학생을
- 도장 (아직 다른 모델에 연결되지 않음) :
has_many :training_times
는 - TrainingTime :
belongs_to :dojo
나는 학생을 원한다 많은 훈련 시간을 가질 수 있고, 많은 학생들을 훈련시키는 시간을 가질 수 있습니다. 내 학생 컨트롤러에서 현재
내가 가진 : 훈련 시간이 현재 학생에 연결되어있는 경우
그때 내가 강조하고 싶은 :로
def show
@student = Student.includes(:senseis).find(params[:id])
@times = TrainingTime.includes(:dojo).order("time ASC")
@times = sort_by_place_and_day(@times)
end
어떤 그때 렌더링 빨간색과 같이 다음과 같이 표시됩니다.
<% if time.students.exists?(@student) %>
<td class="orange">...</td>
<% else %
<td class="normal">...</td>
<% end % >
제 질문은 이렇습니다 : 매번 데이터베이스를 다시 질의 할 것인가? 2 모델을 연관 시키거나 포함시키지 않으려면 어떻게해야합니까?