Autoloading your own libraries with Rails

April 1, 2012

I’m new to Rails (and Ruby, more or less) and am currently working on my first real project with it.

One thing I needed to do was to autoload my own library files, which it seems Rails 3 doesn’t do (but Rails 2 does, from what I found). It took a while to find the “proper” way to do this, so here it is - open config/application.rb and uncomment config.autoload_paths and change the value to whatever you like.

I set it as follows as I thought it’d be a better idea to only include my own files rather than assets and tasks if I had included the entire lib directory.

config.autoload_paths += %W(#{config.root}/lib/my_project)

If anyone knows a better way - let me know!

comments powered by Disqus