In my Rails app, in app/models/, I have a file named array.rb which contains the following code :
class Array
def sum_f
#my code
end
end
When I run the console, Array.new.respond_to?(:sum_f) returns false whereas the test console returns true (and the method works nicely).
I’ve figured out that setting config.cache_classes to true in development.rb fixes this but my guess is that it is not the correct solution.
Any ideas why my modifications to the Array class are being ignored in the development ENV ?
Cheers
Rails, in development, lazily loads files. Which means unless you explicitely require it, the file isn’t loaded until you require it’s class.
What I do is that I create an initializer, like
config/initializers/core_ext.rbWhich includes all my application’s core extensions.
Then, add all your core extensions in the
lib/core_ext(likelib/core_ext/arrayfor your example) directory and they’ll be automatically loaded when you start the application.This also means that every time you change a core extension file, you need to restart your rails server.