I am writing my first Rails gem, which adds a method to ActiveRecord. I can’t seem to figure out a simple way to call other methods from within the method I am adding to ActiveRecord. Is there a pattern for this I should be using?
module MyModule
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def my_class_method
# This doesn't work
some_utility_method
end
end
def some_utility_method
# Do something useful
end
end
ActiveRecord::Base.send(:include, MyModule)
Once you’ve included
MyModule,ActiveRecord::Basewill havemy_class_methodas a class method (equivalently, an instance method of theClassobjectActiveRecord::Base), andsome_utility_methodas an instance method.So, inside
my_class_method,selfis the ClassActiveRecord::Base, not an instance of that class; it does not havesome_utility_methodas an available methodEdit:
If you want a utility method private to the Module, you could do it like this: