2014-10-22 3 views
0

여행에 속한 주제 모델이 있습니다. 여행에는 start_date 및 end_date가 있습니다. 여행 날짜를 기준으로 주제를 찾고 싶습니다. 레일즈에서이 쿼리를 어떻게 설정합니까?관계를 기반으로 한 모델 검색

Topic.joins(:trip).where('trip.start_date > ?', Time.now) 

다음과 같은 오류가 발생합니다.

Topic Load (0.3ms) SELECT "topics".* FROM "topics" INNER JOIN "trips" ON "trips"."id" = 
"topics"."trip_id" WHERE (trip.start_date < '2014-10-22 13:17:37.764743') ORDER BY created_at DESC 
SQLite3::SQLException: no such column: trip.start_date: SELECT "topics".* FROM "topics" INNER JOIN  
"trips" ON "trips"."id" = "topics"."trip_id" WHERE (trip.start_date < '2014-10-22 13:17:37.764743')  
ORDER BY created_at DESC 
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: trip.start_date: SELECT 
"topics".* FROM "topics" INNER JOIN "trips" ON "trips"."id" = "topics"."trip_id" WHERE 
(trip.start_date < '2014-10-22 13:17:37.764743') ORDER BY created_at DESC 

어떻게이 쿼리를 잘못 구성합니까?

+0

내 대답이 도움이 되었습니까? –

+0

예, 감사했습니다! –

답변

1

레일에있는 테이블 이름은 규칙에 따라 복수형 (모델 이름과 반대)이며 여기서는 로그에 따라 다릅니다. 그래야합니다 :

Topic.joins(:trip).where('trips.start_date > ?', Time.now) 
관련 문제