232

I use the following line in an initializer to autoload code in my /lib directory during development:

config/initializers/custom.rb:

RELOAD_LIBS = Dir[Rails.root + 'lib/**/*.rb'] if Rails.env.development?

(from Rails 3 Quicktip: Auto reload lib folders in development mode)

It works great, but it's too inefficient to use in production- Instead of loading libs on each request, I just want to load them on start up. The same blog has another article describing how to do this:

config/application.rb:

# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

However, when I switch to that, even in development, I get NoMethodErrors when trying to use the lib functions.

Example of one of my lib files:

lib/extensions.rb:

Time.class_eval do
  def self.milli_stamp
    Time.now.strftime('%Y%m%d%H%M%S%L').to_i
  end
end

Calling Time.milli_stamp will throw NoMethodError

I realize others have answered similar questions on SO but they all seem to deal with naming conventions and other issues that I didn't to have to worry about before- My lib classes already worked for per-request loading, I just want to change it to per-startup loading. What's the right way to do this?

1
  • Is the config/initializers folder automatically loaded when a Rails app starts up?
    – Jwan622
    Mar 16, 2016 at 14:09

4 Answers 4

550
+100

I think this may solve your problem:

  1. in config/application.rb:

    config.autoload_paths << Rails.root.join('lib')
    

    and keep the right naming convention in lib.

    in lib/foo.rb:

    class Foo
    end
    

    in lib/foo/bar.rb:

    class Foo::Bar
    end
    
  2. if you really wanna do some monkey patches in file like lib/extensions.rb, you may manually require it:

    in config/initializers/require.rb:

    require "#{Rails.root}/lib/extensions" 
    

P.S.

12
  • 1
    @ifyouseewendy- You're exactly right- because extensions.rb wasn't following the Rails naming conventions, Rails wouldn't include it in the loading process. I got it working by manually requiring it.
    – Yarin
    Oct 31, 2013 at 19:38
  • @ifyouseewendy how can I include files before models are loaded? add the path to autoload it's cool, but how control the order of inclusion? thx
    – Matrix
    Oct 16, 2014 at 15:51
  • @Matrix "include files before models are loaded", you may manually require your file without using autoload feature. Oct 17, 2014 at 10:36
  • @ifyouseewendy if I require it in initializer but the file are in autoload_path, it will be reloaded (loaded 2 times) or not? theire is a "require_once" like in php?
    – Matrix
    Oct 17, 2014 at 10:41
  • 5
    This doesn't seem to work in Rails 5 API in production (but does in development). I believe that you need to use config.eager_load_paths << Rails.root.join('lib'). However, that has a major downside in that eager_load_paths loads everything in tasks as well. I think that lulalala's solution is better. Here's a blog post with more details: blog.arkency.com/2014/11/…
    – hirowatari
    Jun 9, 2016 at 15:45
34

Though this does not directly answer the question, but I think it is a good alternative to avoid the question altogether.

To avoid all the autoload_paths or eager_load_paths hassle, create a "lib" or a "misc" directory under "app" directory. Place codes as you would normally do in there, and Rails will load files just like how it will load (and reload) model files.

6
  • 3
    I am in Rails 4.2. and it doesn't auto load files under app, I need to do it manually......or need to put it in the autload path.. Jul 17, 2015 at 16:45
  • 6
    You are wrong, Arup, any subdirectories of app directory are automatically in the autoload_paths array in Rails 4.2.See edgeguides.rubyonrails.org/… Oct 30, 2015 at 8:51
  • Excepting the app/views directory which does not get added; or rather gets explicitly removed. Dec 16, 2015 at 14:21
  • 1
    Great answer. Only thing that worked for me on rails 5/api.
    – jstafford
    Feb 29, 2016 at 4:51
  • 6
    Just remember that lib is meant for code that can be applied to multiple projects and might possibly be extracted to a gem. If not create a more appropriate folder under app search as services/ or presenters/ and even subdirs off these.
    – PhilT
    Apr 14, 2016 at 15:46
6

This might help someone like me that finds this answer when searching for solutions to how Rails handles the class loading ... I found that I had to define a module whose name matched my filename appropriately, rather than just defining a class:

In file lib/development_mail_interceptor.rb (Yes, I'm using code from a Railscast :))

module DevelopmentMailInterceptor
  class DevelopmentMailInterceptor
    def self.delivering_email(message)
      message.subject = "intercepted for: #{message.to} #{message.subject}"
      message.to = "[email protected]"
    end
  end
end

works, but it doesn't load if I hadn't put the class inside a module.

1
  • 1
    In ruby "matching appropriately" means that the file is located in the file system at LOAD_PATH/module/class.rb (underscored) where LOAD_PATH is in the load paths used by the Ruby app (autoload_paths in the case of Rails). lib has fluctuated from being auto loaded by Rails to not being autoloaded, and in recent versions (>= Rails 3.x) it is not autoloaded. Whatever magic is making this work for you is not recommended. Perhaps it is an old Railscast? Sep 22, 2016 at 20:49
0

Use config.to_prepare to load you monkey patches/extensions for every request in development mode.

config.to_prepare do |action_dispatcher|
 # More importantly, will run upon every request in development, but only once (during boot-up) in production and test.
 Rails.logger.info "\n--- Loading extensions for #{self.class} "
 Dir.glob("#{Rails.root}/lib/extensions/**/*.rb").sort.each do |entry|
   Rails.logger.info "Loading extension(s): #{entry}"
   require_dependency "#{entry}"
 end
 Rails.logger.info "--- Loaded extensions for #{self.class}\n"

end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.