Where is the best place to stash Ruby files that add functionality to
Rails classes (in my case, ActiveRecord::Base)? I think my module is
being loaded too late, and causing a class method to be undefined.
Here's what I tried: I wrote a module to add a new class method to
ActiveRecord::Base following a pattern that I picked up from
http://guides.rubyonrails.org/plugins.html; in my module implement the
included callback and have it extend the target class with my class
methods (actually singleton methods on the metaclass of my class
object if I'm understanding all of this weirdness correctly):
base.send :extend, ClassMethods
Then outside of the module definition I tell ActiveRecord::Base to
include my module:
And according to my (obviously poorly written) unit test, this all
assert MyModel.respond_to?(:my_method, true)
class MyModel < ActiveRecord::Base
my_method # undefined local variable or method `my_method'
So it seems reasonable to me that this might happen if my_model.rb is
loaded first, then my module, then the test. Additional evidence is
that if I add a "debugger" line to my included callback, I'm never
sent to the debugger. Unless I remove the call to my_method, then I
So if my hypothesis is correct, it's a bad idea to add my modules to
lib\modules and append that to the end of my load path. But then
where should I stash modules that update rails classes?