开发者

Why ARel query is returned as ActiveRecord::Relation class instead of parent class?

I have a class:

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

In rails console, when I do the following query:

technician = Technician.named("john")  
technician.class =&g开发者_运维百科t; ActiveRecord::Relation and not Technician  

this matters because I get a no method error when I try to access the class attributes:

technician.id => no method error  

what am I doing wrong?


Arel returns ActiveRecord::Relation so that it can defer the execution to the last moment and provide better composability.

Technician.named("john").first instead of Technician.named("john") to get the technician.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜