Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to use rails load paths, app, and lib directories.

In Rails 3

NOTE: This post now lives (and kept up to date) on my blog:

If you add a dir directly under app/

Do nothing. All files in this dir are eager loaded in production and lazy loaded in development by default.

If you add a dir under app/something/

(e.g. app/models/concerns/, app/models/products/)

Ask: do I want to namespace modules and classes inside my new dir? For example in app/models/products/ you would need to wrap your class in module Products.

If the answer is yes, do nothing. It will just work.

If the answer is no, add config.autoload_paths += %W( #{config.root}/app/models/products ) to your application.rb.

In either case, everything will be eager loaded in production.

If you add code in your lib/ directory

Option 1

If you put something in the lib/ dir, what you are saying is: "I wrote this library, and I want to depend on it where I decide." This means that if you use your library in a rake task, but not in a rails app, you just require it in your rake task. If you need this library to always be loaded for your rails app, you require it in an initializer. If you need this library for some of your models or controllers, you require_dependency (see below why) it in those files, and since everything under your app/ dir is already auto- and eager- loaded as needed, your library will only be "pulled-in" if something that requires it from app/ or rake, or your custom script, actually gets loaded.

Option 2 (bad)

Another option is to add your whole lib dir into autoload_paths: config.autoload_paths += %W( #{config.root}/lib ). This means you shouldn't explicitly require your lib anywhere. As soon as you hit the namespace of your dir in other classes, rails will require it. The problem with this is that in Rails 3 if you just add something to your autoload paths it won't get eager loaded in production. You would need to add it to eager_load_paths instead, which causes a different problem (see below). And in ruby 1.9 autoload is not threadsafe. You probably want eager loading in production. Requiring your lib explicitly, like in option 1, is akin to eager loading it, which is threadsafe.

Option 3 (meh)

All the different things under your lib dir should be placed into their own directories, and those directories should be individually added to eager_load_paths.

config.eager_load_paths += %W(

This means that you can't just throw files into your lib dir. If you have my_lib1.rb, you must put it under my_lib1/my_lib1.rb and my_lib1 should be added to eager load paths. This means that if you have more files in my_lib1, you should create a dir my_lib1/my_lib1/extra.rb. This is a bit annoying.

So why not just add lib/ into eager_load_paths?

If you add lib/ into eager_load_paths, everything will work great. Your files will be autoloaded in development, and eager-loaded in production. Except the problem is that eager_load_paths use globbing like lib/**/*.rb, meaning that everything in your lib dir will try to get loaded. Your tasks, your generators, everything. This is not what you want.

Organizing lib

Regardless of which option you pick (option 1, hint hint), in your lib/ dir you should structure your code as if you structure a gem. If you need more than 1 file, you could for example add a same-named directory where everything is properly namespaced, and let your 1 file relatively require files in that directory.

Why use require_dependency (auto-reloading)

If you use require_dependency, you are enabling auto-reload of your files in development across requests. require alone won't do it. I suggested to use it in your rails app, but not in initializers or rake tasks because rake tasks only run once, and changing initializers always requires restart.

However, it won't work without one additional piece of configuration. In application.rb you should add this:

config.watchable_dirs['lib'] = [:rb]
Copy link

lesterz commented Nov 12, 2013

Thank you! This posting finally explained what I was looking for.

Copy link

I still can't figure out where to put/how to load Devise customised strong_parameters for Rails 4.0. At Devise Wiki, it is explained how but not where to put the below code:

class User::ParameterSanitizer < Devise::ParameterSanitizer
  def sign_in
    default_params.permit(:username, :email)

I tried all the proposed solutions but none of them worked, - I just never enter inside the above class. Do you have any idea ?
Actually I put it in models folder as follows: models/user/parameter_sanitizer.rb and it worked. Any better idea, may be to put in lib folder ?

Copy link

Awesome post, very well explained and makes total sense.

Copy link

kelso commented Apr 30, 2014

I am amazed! Thank you very much for this article, it's very well explained.

Copy link

Tried using the Option 1. Throws an error in production on heroku saying "No such file to load" for the file used from lib folder. Any suggestions ?

Copy link

This was driving me crazy in development. require_dependency is exactly what I wanted. 👍

Copy link

barberj commented Sep 21, 2014

All files in this dir are eager loaded in production and lazy loaded in development by default. is there a config to make them eager loaded in production?

Copy link

hhameed commented Apr 23, 2016

thanks a lot man, you saved me a lot of time and headache......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment