我正在尝试构建一个gem,我想在gem中定义一个方法my_method
并在模型中使用它。
示例:
class MyModel < ActiveRecord::Base
my_method
end
我的宝石:
#lib/my_gem.rb
require "my_gem/model_inclusions"
module MyGem
end
#lib/my_gem/model_inclusions.rb
module MyGem
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def my_method
end
end
end
当我尝试这个例子时,它会给我undefined method 'my_method' for <Class:0x00000045434> (NoMethodError)
答案 0 :(得分:2)
module NumberInternationalizer
def my_method
...
end
end
ActiveRecord::Base.send :extend, NumberInternationalizer