Rails 3 Autoload Modules/Classes – 3 Easy Fixes

Rails 3 Autoload Modules/Classes – 3 Easy Fixes

Judging by the number of queries on Stack Overflow, it is a very common problem for folks using Rails 3 that they are not getting the modules or classes from their lib directory loaded in Rails 3, especially in production. This leads to errors like “NoMethodError,” “No such file to load [file in your lib path],” and “uninitialized constant [thing you’re trying to load].”  From the information I canvassed trying to solve the problem this morning, there are three common reasons (and one uncommon reason) that this can happen. I’ll go in order of descending frequency.

1. You haven’t included the lib directory in your autoload path

Rails 3 has been updated such that classes/modules (henceforth, C/M) are lazy loaded from the autoload paths as they are needed.  A common problem, especially for the newly upgraded, is that the C/M you are trying to load isn’t in your autoload path.  Easy enough fix:

config.autoload_paths += Dir["#{config.root}/lib/**/"]

This will autoload your lib directory and any subdirectories of it. Similarly, if you have model subdirectories, you may need to add

config.autoload_paths += Dir["#{config.root}/app/models/**/"]

To your application.rb file.

See also: 6 Guidelines for choosing the best SEO Agency in Australia

2.  Your C/M is not named in the way Rails expects

Rails has opinions about what the name of your C/M should be, and if you name it differently, your C/M won’t be found.  Specifically, the namespace for your C/M should match the directory structure that leads to it.  So if you have a C/M with the filename “search.rb” in the lib/google directory, it’s name should be “Google::Search.”  The name of the C/M should match the filename, and the name of the directory (in this case “Google”) should be part of the namespace for your module.  See also this.

3.  Your application is threadsafe.

This was one that was hard to find on SO or Google, but if you have configured your application to be threadsafe (done by default in production.rb), then the C/Ms you have, even if they exist in your autoload path, will not be defined until you require them in your application.  According to the Rails docs, there is apparently something not threadsafe about automatically loading the C/Ms from their corresponding files.  To workaround this, you can either require your individual library files before you use them, or comment out config.threadsafe!

4.  (Esoteric) You have a model in a subdirectory, where the name of the subdirectory matches the name of the model.

Yes, this is another error we actually had when upgrading from Rails 2 to 3:  as of Rails 3.0.4 there is a bug wherein, if you have a directory app/models/item and you have the file “item.rb” in your app/models/item directory, then you will get errors from Rails that your models are undefined (usually it picks the second model in the item subdirectory).  We fixed this by just renaming our subdirectories where appropriate.  Hopefully in one of the next versions of Rails this will get fixed and no longer be a possible problem.

Had other reasons why C/Ms failed to load for you?  Do post below and we can gather up all the possibilities in one tidy location.

Back to top