2014-02-06 3 views
0

가입 :레일 액티브는 콘솔에서이 코드를 혼란

Patient.joins(:notes,:recordings).find(1) 

반환하기 때문에 어떤 문제없이 작동하고 ID를 가진 환자의 레코드를 검색 Patient.find(1) 같은 콘솔에서 기괴한

ActiveRecord::RecordNotFound: Couldn't find Patient with id=1 

1.

내 이해는 내가 할 수 있어야한다는 것입니다 :

a = Patient.joins(:notes,:recordings).find(1) 
a.notes 
a.recordings 

그리고 a.notes는 a.recordings와 동일하게 id가 1 인 환자와 관련된 모든 메모를 반환해야합니다. 내가 여기서 뭔가를 놓치고있는 것이 분명해 ... 어떤 생각?

답변

2

레일스는 기본적으로 INNER JOIN을 사용하기 때문입니다. 환자 1notes 또는 recordings

왼쪽 가입을 대신 수행 할 수있는 경우. 당신은에 관심이있을 수

Patient.joins("LEFT JOIN notes on notes.patient_id = patients.id") 
     .joins("LEFT JOIN recordings on recordings.patient_id = patients.id") 
     .find(1) 

또는로드 한 후 환자를로드하고 협회

a = Patient.find(1) 
a.notes 
a.recordings 
+0

find find (1) to first? – Phlip

+0

id가 존재하지 않으면 오류를 던지기보다 빈 연결을 반환 할 수있는 방법이 있습니까? – Morgan

+0

Nvm; 그것을 해결했습니다. – Morgan