I want to reopen the ActiveRecord::Base class in a Rails environment
and is wondering about the best place to put the code. The new and
altered methods should be used by all instances in the whole Rails
application. Any ideas?
I have a question on it why it has to be in initializers. I did try to have to methods for the Object class and have to add that file in initializer. Do you guys figure out any specific reason for it?
One not technical reason for putting them in the initializers is the purpose of the file.
Because overwriting ActiveRecord is a one time task, it make sense to put it there, I think.
But, for the technical reason, you could put the file not in the initializers itself, but you could locate a folder inside app and put it there, but you have to follow the rules of autoloading, in particular the rule of the naming of the files.
That rule was the main one that was forbidding me to put code in the app/lib or app/whatever folders: you have to match the name of the class with the name of the file and the name of a module with the name of a folder (now that I think about it, you are forced to write classes, like if Ruby was not already forcing you to write them anyways).
e.g: the following example would be autoloaded correctly:
# app/services/example/one.rb
module Example
class One
end
end
Files in the config/initializers folder are not tied to this rule but tied to the rules of initializers. That don’t know them 100%, except you have to stop spring (with spring stop) to reload the file if changed.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.