给定以下代码:
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
员工.rb
module Refinery
class Employee < ActiveRecord::Base
belongs_to :user, :class_name => 'Refinery::User'
scope :active, where(:retired => false)
end
end
运行 Rails 控制台...
> ::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>
我究竟做错了什么?