简体   繁体   English

无法使用class_eval方法添加ActiveRecord回调

[英]Can not add ActiveRecord callback by using class_eval method

I have a User model, which includes module Staff: 我有一个用户模型,其中包括模块Staff:

class User < ActiveRecord::Base
  include Staff
  ...
end

I want to add an after_update callback to all the models that include this module: 我想向包含此模块的所有模型添加一个after_update回调:

module Staff
  def self.included(model)
    model.class_eval do
      after_update :callback
    end
  end

  private
  def callback
    ...
  end
end

I get a NoMethodError: 我收到一个NoMethodError:

undefined method `after_update' for Object:Class

What am I doing wrong? 我究竟做错了什么?

Solved! 解决了! The problem was that I had another class including my module Staff, which was not an ActiveRecord model. 问题是我还有另一个类,包括模块Staff,它不是ActiveRecord模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM