Ruby on rails 如何在类方法上使用ActiveModel回调?
我想在每个类方法之前执行一个方法。是否可以使用ActiveModel回调或必须使用元编程来实现这一点 我试过:Ruby on rails 如何在类方法上使用ActiveModel回调?,ruby-on-rails,activemodel,Ruby On Rails,Activemodel,我想在每个类方法之前执行一个方法。是否可以使用ActiveModel回调或必须使用元编程来实现这一点 我试过: class Image extend ActiveModel::Callbacks define_model_callbacks :create before_create :set_base_uri def self.create run_callbacks :create do ..my_code.. end end def
class Image
extend ActiveModel::Callbacks
define_model_callbacks :create
before_create :set_base_uri
def self.create
run_callbacks :create do
..my_code..
end
end
def set_base_uri
..my_code..
end
end
当我调用Image.create时,我得到
Image:Class的未定义方法“run\u回调”
如果我将create更改为实例方法,同样的代码也可以工作。我查看了,但他们没有提到类方法,ActiveModel是否支持类方法回调?我认为回调不起作用,因为回调方法是在实例级别调用的。使用别名可能有效