I’m working on an ApplicationModel for a Rails app that has over 200 modules. The app is so large that when we rails s it literally takes 40 seconds to load our app. So in development we have a ton modules we often change and to see the changes we have to reload the app.
So I created an ApplicationModel and moved all the initializers into that model so changes are reloaded in development and we do not have to restart the server again saving lots of time everyday.
I made ApplicationModel inherit from ActiveRecord::Base and made it an abstract class. All other models inherit from this model. So now we can put our initializers that extend ActiveRecord into this model.
Since I’m working on a project that other people started I don’t know the difference between these two ways of extending ActiveRecord and would like to know the differences between them.
ActiveRecord::Base.send(:include, someModule)
ActiveRecord::Base.extend(ClassMethods)
First, I think you’re overcomplicating this a bit. If you want to reload your application’s models in the console, the best choice is to just run
reload!, not a complicated custom solution.Second, the difference between these two choices:
is that
includeadds the module to the class, just as if you’d copy-pasted the instance methods in the module right into the class’s definition.By contrast,
extendwill add the module to the metaclass, just as if you’d copy-pasted the instance methods in the module into aclass << selfblock in the class.Here’s an illustrative example:
But: