1

안녕하세요, has_one 연관이있는 두 모델이 있습니다. 위임 필드로 검색해야합니다.모델의 대리자 필드가있는 레일 쿼리

**Model 1:** 
class Reservation < ActiveRecord::Base 
    belongs_to :content 
    delegate :type, :title, :to => :content 
end 

**Model 2:** 
class Content < ActiveRecord::Base 
    has_one :reservation 
end 

다음 쿼리 때문에 대표의 잘 작동 :

reservations = Reservation.last 
reservations.title 
~ Content Load (0.6ms) SELECT `contents`.* FROM `contents` WHERE `contents`.`id` = 95 LIMIT 1 
=> "Birthday Party" 

지금 내가 위임 필드를 사용하여 쿼리를 수행해야합니다

reservations = Reservation.where("title = ?","some_title") 

는 반환 오류 :

Unknown column 'title' in 'where clause' 

어떻게해야합니까? 문제를 풀다? 그것은 올바른 방법으로하고 있습니까? 내 질문을 읽어 주셔서 감사합니다.

답변

1

joinswhere을 사용하여 문제를 해결할 수 있습니다. 그러나 이것이 당신이 찾고있는 것인지 확실하지 않습니다.

reservations = Reservation.joins(:content).where("contents.title = ?","some_title") 
+0

"content.title"을 "title"로 변경하여 작동하는이 쿼리는 –

+0

작동합니다. 그러나 Reservation에도 같은 열이 있으면 충돌이 발생하기 때문에 접두어로 테이블 이름을 사용하는 것이 좋습니다. – shiva

+0

접두사로 테이블 이름을 사용하면이 오류가 발생합니다. MySQL : 오류 : 알 수없는 열 'content.title' –