0

给定以下代码:

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>

我究竟做错了什么?

4

1 回答 1

1

Ascope与模型相关,与该模型的实例无关。

如果你想这样做,你必须创建一个做同样事情的方法。

至少,我是这么认为的……

于 2013-05-10T14:37:08.913 回答