2010-11-20 8 views
1

나는 클래스가 :왜 ARel 쿼리가 부모 클래스 대신 ActiveRecord :: Relation 클래스로 반환됩니까?

레일 콘솔에서
class Technician < ActiveRecord::Base 
    scope :named, lambda {|name| where(["first_name LIKE ?", "%#{name}%"])} 
end 

, 나는 다음과 같은 쿼리를 수행 할 때 I 클래스 속성에 액세스하려고 할 때 나는 어떤 방법 오류가 발생하기 때문에

technician = Technician.named("john") 
technician.class => ActiveRecord::Relation and not Technician 

이 중요한 :

technician.id => no method error 

무엇이 잘못 되었나요?

답변

4

Arel은 ActiveRecord::Relation을 반환하므로 실행을 마지막 순간까지 연기하고 더 나은 조합을 제공 할 수 있습니다.

Technician.named("john").firstTechnician.named("john") 대신 technician을 얻으십시오.

+0

찬드라 ... 도와 줘서 고마워. 내가 그 뉘앙스를 알아 내는데 영원히 걸렸을 것이다. –

관련 문제