2016-09-30 3 views
0

나는 방법 has_one 관계를 확장하는 시도했다 :액티브`has_one` 협회 확장 방법

has_one :subscription_plan, -> { where current: true } do 
    def in newspaper 
    where newspaper: newspaper 
    end 
end 

및 오류 undefined method 'in' for #<SubscriptionPlan...>

있어하지만 has_many와 함께 작동 :

has_many :subscription_plans, -> { where current: true } do 
    def in newspaper 
    where(newspaper: newspaper).take 
    end 
end 

Rails 4.2.1에서 has_one으로 작동시키는 방법은 무엇입니까?

답변

0

has_many에 액세스하면 SubscriptionPlan::ActiveRecord_Associations_CollectionProxy이 표시되지만 has_one이면 SubscriptionPlan 인스턴스가 수신됩니다.

여기에는 확장 할 항목이 없습니다.

모델에 대한 자세한 정보를 제공한다면 정확한 코드를 알려줄 것입니다.

+0

많이 좋아요! ActiveRecord 문서 http://guides.rubyonrails.org/association_basics.html#association-extensions는 다소 혼란 스럽습니다.'확장 기능은 proxy_association 접근 자의 세 가지 속성을 사용하여 연결 프록시의 내부를 참조 할 수 있습니다. ... proxy_association. target은 belongs_to 또는 has_one에 대한 연관된 ​​객체 또는 has_many 또는 has_and_belongs_to_many에 대한 연관된 ​​객체의 컬렉션을 반환합니다. 문서에서 'has_one'이 언급 된 것은 실수입니까? – yurko

+0

나는 그것이 오류라고 생각하지 않지만, 지금은 주석을 달 수 없다. =) – Aleksey