无法从mixin访问with_scope

时间:2010-02-09 18:01:21

标签: ruby-on-rails mixins

我只是偶然发现了一个奇怪的问题,我真的不明白是什么造成了这个问题。

在我们的rails应用中,让我们有一个mixin Mixin

module Mixin
  def foo
    with_scope :find => ... do
      ...
    end
  end
end

include在其他地方加入了模型类:

class Model < ActiveRecord::Base
  include Mixin
  ...
end

调用Model.new.foo会导致错误:NoMethodError: undefined method with_scope

然后我将foo方法更改为:

  def foo
    self.class.with_scope :find => ... do
      ...
    end
  end

但这也会导致错误:NoMethodError: protected method with_scope called

这看起来很奇怪。我原以为mixin方法的行为与Model中的任何其他方法一样。我以前从来没有偶然发现过这个问题,因为所有像save这样的实例方法都在那里,并照常工作。

我做错了吗?

0 个答案:

没有答案
相关问题