我无法从 Google 上读到的内容中弄清楚这一点,但我想制作一个 gem,它可以在保存时改变模块的行为,但我不知道该怎么做。我如何在 Gem 中定义一个覆盖模型保存方法的保存方法?
更新:我发现 Rails 3: alias_method_chain still used? ,我将对此进行检查。 Rails 3 似乎已弃用 alias_method_chain。
最佳答案
我宁愿这样做:
module YourModule
def self.included(base)
base.extend(InstanceMethods)
end
module InstanceMethods
def save
# Your behavior here
super # Use this if you want to call the old save method
end
end
end
然后在模型中:
class User < ActiveRecord::Base
include YourModule
end
希望有帮助:)
关于ruby-on-rails - Gem, rails 的自定义保存方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913583/