考虑以下代码:
user_decorator.rb
Refinery::User.class_eval do
has_many :employees, :class_name => 'Refinery::Employee'
scope :active_employees,
lambda {
joins(:employees).merge(::Refinery::Employee.active)
}
end
employee.rb
module Refinery
class Employee < ActiveRecord::Base
belongs_to :user, :class_name => 'Refinery::User'
scope :active, where(:retired => false)
end
end
运行rails console ...
> ::Refinery::User.active_employees
=> [ .......... lots of records etc ]
> user = ::Refinery::User.where(:username => 'test').first
=> #<Refinery::User .... etc etc>
> user.active_employees
NoMethodError: undefined method `active_employees' for #<Refinery::User:0x000000051330f8>
我做错了什么?
答案 0 :(得分:1)
scope
与模型有关,而与该模型的实例无关。
如果你想这样做,你必须创建一个同样的方法。
至少,我想是这样......