문제

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 => 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top