如果我做这样的事情:
class ActiveRecord::Base
def self.encrypt(*attr_names)
encrypter = Encrypter.new(attr_names)
before_save encrypter
after_save encrypter
after_find encrypter
define_method(:after_find) { }
end
end
require
吗?require
?做您想做的事情的轨范方式是:在lib / encryptable.rb中创建一个文件(如果您在rails 4上,则在app / models / concerns中),该文件定义了您的方法模块。然后,您可以在模型中执行include Encryptable
或(对于所有模型)在初始化程序中:
ActiveRecord::Base.class_eval do
include Encryptable
end
在此处阅读有关Rails 4的更多关注点:如何在Rails 4中使用关注点
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句