2010-11-20 49 views
1

我有一個類:爲什麼ARel查詢返回爲ActiveRecord :: Relation類而不是父類?

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

在軌控制檯,當我做下面的查詢:

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

這很重要,因爲我得到一個無方法錯誤,當我嘗試訪問類的屬性:

technician.id => no method error 

我做錯了什麼?

回答

4

Arel返回ActiveRecord::Relation,以便它可以將執行推遲到最後一刻並提供更好的組合性。

Technician.named("john").first而不是Technician.named("john")得到technician

+0

錢德拉...感謝您的幫助。這本身會讓我永遠想到這個細微差別。 – 2010-11-20 21:20:39

相關問題